Python CSV Error -


hello keep getting error while making small program sort large csv files out, below code , error, doing wrong?

if selection:         stuff in stuffs:              try:                 textfile = open("output.txt",'w')                 mycsv = csv.reader(open(stuff))                 d_reader = csv.dictreader(mycsv)                 headers = d_reader.fieldnames <-- error happens here                 if selection in headers:                     placeinlist = headers.index(selection)                      #placeinlist = selection.index(selection)                     selection in tqdm(mycsv, desc='extracting column values...', leave = true):                         textfile.write(str(selection[int(placeinlist)])+'\n')                     print 'done!'                     textfile.close()                     sys.exit()             except ioerror:                 print 'no csv file present in directory'                 sys.exit()     else:         sys.exit() 

and error:

traceback (most recent call last):  file "postcodeextractor.py", line 27, in <module> headers = d_reader.fieldnames  file "c:\python27\lib\csv.py", line 90, in fieldnames self._fieldnames = self.reader.next()  typeerror: expected string or unicode object, list found 

instead of

mycsv = csv.reader(open(stuff)) d_reader = csv.dictreader(mycsv) 

you want

d_reader = csv.dictreader(open(stuff)) 

the first line problem.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -