gpt4 book ai didi

python - 在 Python 中搜索文本文件中的单词

转载 作者:太空宇宙 更新时间:2023-11-04 08:47:44 25 4
gpt4 key购买 nike

我有这个文本文件:

MemTotal,5,2016-07-30 12:02:33,781
model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075

我需要找到模型的线。

我的代码:

term = "model"
file = open('file.txt')
for line in file:
line.strip().split('/n')
if term in line:
print line
file.close()

这是输出:

model name,3,2016-07-30 13:37:59,074
model,3,2016-07-30 15:39:59,075

我只需要这一行作为输出:

 model,3,2016-07-30 15:39:59,075

我该怎么做?

最佳答案

只需替换行:

if term in line:

连线:

if line.startswith('model,'):

关于python - 在 Python 中搜索文本文件中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38695194/

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