我正在尝试阅读并打印以'VISIT'开头的所有行,但是下面的代码一直忽略第一行,我搜索了一下并了解到它开始搜索时它位于第二行,我尝试添加f.seek(0)
,但它无效。
with open("visit_log.txt") as f:
lines = f.readlines()
for i, line in enumerate(lines):
if line.startswith('VISIT'):
for l in lines[i]: print l,
print
我的文字档案:
VISIT 82.81.123.011 Israel
VISIT 82.81.123.001 Israel
VISIT 82.81.123.002 Israel
VISIT 82.81.123.003 Israel
VISIT 82.81.123.004 Israel
AVISIT 82.81.123.456 Israel
bVISIT 82.81.123.456 Israel
cVISIT 82.81.123.456 Israel
LLLLL 888.888.888.888 Israel
VISIT 55 82.81.123.005 Israel
________________
VISIT 82.81.123.006 Israel
答案 0 :(得分:2)
我们真的需要readlines()
吗?
with open("visit_log.txt") as f:
for line in f:
if line.startswith('VISIT'):
print line