【发布时间】:2021-03-02 12:54:50
【问题描述】:
编写一个 Python 程序(版本 3)来计算一个或多个 csv 文件的每一行中指定字段中的字符串。
csv 文件包含的位置:
Field1, Field2, Field3, Field4
A, B, C, D
A, E, F, G
Z, E, C, D
Z, W, C, Q
脚本被执行,例如:
$ ./script.py 1,2,3,4 file.csv
结果是:
A 10
C 7
D 2
E 2
Z 2
B 1
Q 1
F 1
G 1
W 1
错误 脚本被执行,例如:
$ ./script.py 1,2,3,4 file.csv file.csv file.csv
错误发生的地方:
for rowitem in reader:
for pos in field:
pos = rowitem[pos] ##<---LINE generating error--->##
if pos not in fieldcnt:
fieldcnt[pos] = 1
else:
fieldcnt[pos] += 1
TypeError:列表索引必须是整数或切片,而不是 str
谢谢!
【问题讨论】:
标签: python-3.x linux parsing typeerror counting