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

标签 python python-2.7

我有这个文本文件:

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/

相关文章:

python - 获取序数变量的虚拟变量并自动更改列名称?

python - 在 Python 中执行访问单独数据库的 MySQL 查询

python - Kivy - 添加和删除标签

python - TestDome 联赛表 - Python

Python2.7 : How to create bar graph using index and column information?

python - Matplotlib:如何在使用 plt.figure() 和 figure.addaxes() 时显示图像颜色条

python - 标记化 python 源代码示例(在 Python 中)

python - 在函数上使用 `__get__` 有什么作用?

python - 如何在pyqt中更改Qtablewidget的特定单元格背景颜色

python - 在没有时间的python中创建日期