既然您提到您必须在不使用 csv 模块的情况下执行此操作:
z_counts = []
lines = []
with open('C://foo.csv', 'r') as f:
lines = f.readlines() #This is your list of all the rows/lines (now populated with data)
for index, line in enumerate(lines):
#store the z counts and the index of the line as pairs using a tuple: `( ... )`
z_counts.append((line.strip().split('z').count('z'), index))
#Since you want the top 5 matches
#Reverse is necessary since Python automatically sorts in ascending order
print sorted(z_counts, reverse=True)[:5]
使用以下示例数据:
a,b,c,d,e,f,g,h,j,i,p
a,e,f,g,h,d,e,g,z,g,z
z,z,z,c,x,s,e,f,d,s,f
q,e,r,s,f,t,y,y,u,i,f
e,p,l,l,k,k,z,z,q,e,r
o,i,i,j,l,s,w,e,r,q,g
w,e,r,f,g,s,v,h,d,b,z
t,r,y,e,u,i,o,p,d,f,j
如果您观察到,其中具有以下 z 计数:
0
2
3
0
2
0
1
0
我们获得以下输出(具有已排序相应 z 计数的元组列表,以及该 z 计数的索引):
[(3, 2), (2, 4), (2, 1), (1, 6), (0, 7)]
输出:
让我们打印出来看看它是什么样子的:
for tpl in sorted(z_counts, reverse=True)[:5]:
print lines[tpl[1]]
哪个输出:
z,z,z,c,x,s,e,f,d,s,f #3 z's, line index 2 (3rd line)
e,p,l,l,k,k,z,z,q,e,r #2 z's, line index 4 (5th line)
a,e,f,g,h,d,e,g,z,g,z #2 z's, line index 1 (2nd line)
w,e,r,f,g,s,v,h,d,b,z #1 z, line index 6 (7th line)
t,r,y,e,u,i,o,p,d,f,j #0 z's, line index 7 (8th line)
正如预期的那样。