0
大家好,我一直在收集這個錯誤,同時使一個小程序來排序大的CSV文件,下面是我的代碼和錯誤,我做錯了什麼?Python CSV錯誤
if selection:
for 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)
for 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()
和錯誤:的
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
你能告訴我們錯誤日誌嗎? – harshil9968
什麼行顯示此錯誤? –
回溯(最近通話最後一個): 文件 「postcodeExtractor.py」,第27行,在 頭= d_reader.fieldnames 文件 「C:\ Python27 \ LIB \ csv.py」,行90,在字段名稱 自我._fieldnames = self.reader.next() 類型錯誤:預期字符串或Unicode對象列表中找到 –
nagrom97