.txt 文件包含 3 个字段,以逗号 (,) 分隔的文本、数字和一些特殊字符,仅运行一个for row in sortedlist: print row[1]
示例就可以正常工作。
import csv
import operator
import sys
inf = csv.reader(open('data.txt','r'))
sortedlist = sorted(inf, key=operator.itemgetter(2), reverse=True)
def dothis(x):
for row in sortedlist:
print row[x]
if __name__ == "__main__":
c = sys.argv[0]
if (c == ''):
raise Exception, "missing first parameter - row"
dothis(c)
我收到以下错误 -
python test.py 1 Traceback (most recent call last): File "test.py", line 16, in ?
dothis(c) File "test.py", line 10, in dothis
print row[x] TypeError: list indices must be integers
我究竟做错了什么?