如果我有一个名为' names.txt'的文件包含以下内容:
Adam Jones
Mary Smith
Matt Good
Huey Lee
Matt Armstrong
我如何只用马特'打印线条? (不使用行号)?
答案 0 :(得分:2)
with open("names.txt", "r") as f:
for line in f:
if line.startswith("Matt"):
print line.strip()
答案 1 :(得分:1)
with open("names.txt") as txt:
[line for line in txt.readlines() if "Matt" in line]
但是这将返回带有Matt的行list
答案 2 :(得分:0)
试试这个:
with open("names.txt") as names:
for row in names:
if row.startswith("Matt"):
print(row.strip())
答案 3 :(得分:0)
使用find
:
f=open('file.txt')
for x in f:
if x.find('Matt')>=0:
print(x)