fhand=open(file)
n2=0
for line in fhand:
if line .startswith("1/15/2014"):
n2=n2+1
print n2
n1=0
for line in fhand:
n1=n1+1
print n1
答案 0 :(得分:0)
循环非常相似;只需将代码放在另一个内:
for line in fhand:
n1 += 1
if line.startswith("1/15/2014"):
n2 += 1
print n2, n1
正如danidee所提到的,打开文件时应始终使用with
:
with open(file) as fhand:
for line in fhand:
n1 += 1
if line.startswith("1/15/2014")
n2 += 1
print n2, n1