The .txt file contains 3 fields comma (,) seperated text, numbers and some special characters, running just a for row in sortedlist: print row[1] for example works fine.
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)
I am receiving the following error –
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
What am I doing wrong?
In the code you show,
cand thereforexare set tosys.argv[0], which is a string. You can’t use a string to index into a list.Did you mean to say: