1.python如何忽略文件的第一行,然后统计剩下部分中某个字符串的?
f = open("foo。
txt") ? ? ? ? ? ? # 打开文件 line = f。readline() ? ? ? ? ? ? # 读第一行 line = f。
readline() ? ? ? ? ? ? # 读第二行 ct=0; ? ? ? ? ? ? ? ? ? ? ? ? ? # 计数 while line: ? ?line = f。 readline() ? ?ct =line。
count("name") ? ? ?# 逐行统计,要找的字串为name f。close() print (ct) ? ? ? ? ? ? ? ? ? ? ?#输出结果 追答 : 有个小问题,while中两句写反了,正确的f = open("foo。
txt") # 打开文件 line = f。readline() # 读第一行line = f。
readline() # 读第二行ct=0; # 计数 while line: ct =line。 count("name") # 逐行统计,要找的字串为name line = f。
readline()f。close()print (ct) #输出结果。
转载请注明出处编程代码网 » python批量查询