我需要将csv转换为列表并计算行中'z'的数量,并打印出5行的行标题,其中包含最大量的Z.此外,更多帮助的链接将是伟大的!谢谢
这是我目前的代码
economistData = open('C:/foo.csv','r')
economistDataList = []s
for line in economistData:
economistDataList.append(line.split(','))
for row in economistDataList:
rowcnt = row.count('z')
答案 0 :(得分:1)
既然你提到你必须这样做而不使用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)
正如所料。