我有一個包含字段名稱的電子表格:添加到現有的電子表格?
['name','occupation','company','address','address_2','city','state','zip','phone' '電子郵件','網站','描述']
並希望在此電子表格中添加其他包含較少字段名的數據電子表格(儘管所有其他字段名都包含在此電子表格中) 。
我得到一個奇怪的錯誤:
Samuel-Finegolds-MacBook-Pro:~ samuelfinegold$ /var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup\ At\ Startup/merge-395698810.980.py.command ; exit;
['name', 'occupation', 'company', 'address', 'address_2', 'city', 'state', 'zip', 'phone,fax', 'email', 'website', 'description']
Traceback (most recent call last):
File "/Users/samuelfinegold/Documents/noodle/merge.py", line 14, in <module>
gc_all_dict.writerow(row)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 148, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/csv.py", line 144, in _dict_to_list
", ".join(wrong_fields))
TypeError: sequence item 0: expected string, NoneType found
logout
[Process completed]
當我運行以下命令:
import csv
# compile master spreadsheet
with(open('gc_all.txt','w')) as gc_all:
fieldnames = ['name', 'occupation', 'company', 'address', 'address_2','city', 'state', 'zip', 'phone,' 'fax', 'email', 'website', 'description']
gc_all_dict = csv.DictWriter(gc_all, fieldnames = fieldnames, delimiter = '\t')
print gc_all_dict.fieldnames
with(open('/Users/samuelfinegold/Documents/noodle/aicep/aicep_scrape_output.txt', 'rU')) as aicep:
aicep_dict = csv.DictReader(aicep, fieldnames = fieldnames, delimiter = '\t')
for row in aicep_dict:
# print row
gc_all_dict.writerow(row)
for row in gc_all:
print row
假數據:
name occupation company address address_2 city state zip phone fax email website description
Rob Er Step Up 123 Road Dr New York NY 10011 1234567891 1234567891 [email protected] www.stepUp.com A great counselor
Bob B. Bob For Your Rights 12 2nd Ave San Francisco CA 94109 1234567891 1234567891 [email protected]
Snob Job Marley Inc. 12 1st Ave Denver CO 80231 1234567891 1234567891 [email protected] What a counselor!
你可以給我們一個小樣本aicep_scrape_output.txt文件,以便我們可以調試嗎? – abarnert
有人可能已經問過這個問題,但是有沒有理由不使用像[pandas](http://pandas.pydata.org)這樣的更高級別的庫? – DSM
@DSM:一般來說問題不錯......但如果他所做的只是將異構CSV連接到Excel中使用的超集CSV中,pandas將不會添加任何有用的東西(除非出現雙倍意外時可能會出現更好的錯誤消息發生…)。 – abarnert