gpt4 book ai didi

Python-从文件中读取变量的值

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:48 24 4
gpt4 key购买 nike

在 bash 中,我有一个以可变格式存储密码的文件。

例如

cat file.passwd
password1=EncryptedPassword1
password2=EncryptedPassword2

现在,如果我想使用 password1 的值,这就是我需要在 bash 中执行的操作。

grep password1 file.passwd  | cut -d'=' -f2

我正在寻找 python 中的替代方案。是否有任何库提供简单提取值的功能,或者我们是否必须手动执行此操作 像下面这样吗?

with open(file, 'r') as input:
for line in input:
if 'password1' in line:
re.findall(r'=(\w+)', line)

最佳答案

读取文件并添加检查语句:

if line.startswith("password1"):
print re.findall(r'=(\w+)',line)

代码:

import re
with open(file,"r") as input:
lines = input.readlines()
for line in lines:
if line.startswith("password1"):
print re.findall(r'=(\w+)',line)

关于Python-从文件中读取变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43474608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com