我試圖從CSV文件中提取一堆行並將它們寫入另一個行,但我遇到了一些問題。Python CSV DictReader/Writer問題
import csv
f = open("my_csv_file.csv", "r")
r = csv.DictReader(f, delimiter=',')
fieldnames = r.fieldnames
target = open("united.csv", 'w')
w = csv.DictWriter(united, fieldnames=fieldnames)
while True:
try:
row = r.next()
if r.line_num <= 2: #first two rows don't matter
continue
else:
w.writerow(row)
except StopIteration:
break
f.close()
target.close()
運行此,我得到以下錯誤:
Traceback (most recent call last):
File "unify.py", line 16, in <module>
w.writerow(row)
File "C:\Program Files\Python25\lib\csv.py", line 12
return self.writer.writerow(self._dict_to_list(row
File "C:\Program Files\Python25\lib\csv.py", line 12
if k not in self.fieldnames:
TypeError: argument of type 'NoneType' is not iterable
不能完全確定什麼,我錯了洞。
輝煌記錄。我確實傾向於過分複雜的東西。謝謝。 – saturdayplace 2009-07-29 20:37:56
這個答案很精彩,因爲它解決了潛在的問題;) – Jaykul 2013-02-19 23:19:35