在几个文件中搜索2个变量
我正在尝试在几个基于文本的文件中找到2个值。 我拥有的代码是:
def newbat():
query1 = "Dump/data/log/batterystats/newbatterystats*"
name1 = "plug=ac"
strt = "RESET"
dus = os.path.join(path, query1)
entries1 = glob.glob(dus, recursive=True)
for entry in entries1:
with open(entry, 'r') as file:
for line in file:
if name1 in line:
outs3 = os.path.join(path, 'newbatterystat.txt')
sys.stdout = open(outs3, 'a')
print(entry + '\n', line)
name1变量工作。 如果我将name1替换为strt,则可以正常工作。 我找不到如何使用两个变量,因此输出文件是: 重置:时间:01-01-1970插头= AC
I'm trying to find 2 values in several textbased files.
The code i have is:
def newbat():
query1 = "Dump/data/log/batterystats/newbatterystats*"
name1 = "plug=ac"
strt = "RESET"
dus = os.path.join(path, query1)
entries1 = glob.glob(dus, recursive=True)
for entry in entries1:
with open(entry, 'r') as file:
for line in file:
if name1 in line:
outs3 = os.path.join(path, 'newbatterystat.txt')
sys.stdout = open(outs3, 'a')
print(entry + '\n', line)
The name1 variable works.
If I replace name1 to strt it works.
I cannot find out how to use both variables, so the output file is:
RESET:TIME: 01-01-1970 plug=ac
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确定有很多方法可以解决您的问题,我建议您使用
REGEX
:I'm sure there are many ways to solve your problem, I would recommend you to use
regex
:真的不需要正则是。
我会为您需要搜索的每个文件做这样的事情。
第一个示例,如果两个单词都必须在线中。第二个示例,如果一个单词的单词必须排成一行。
输出:
No need for regex really.
I would do something like this for each file you need to search in.
First example if both words has to be in the line. Second example if one word of a list of words has to be in line.
Output:
好吧,我解决了它,但不是最优雅的方式。
输出是文件名,重置时间和插头= AC。
看来IF在施工中没有比1个变量更高。
Well I solved it, but not the most elegant way.
The output is filename, reset time and plug=ac.
It appears that the IF IN construction does not take more then 1 variable.