我正在尝试写入文件,但出现以下错误:
Traceback (most recent call last):
File "/private/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup At Startup/merge-395780681.888.py", line 151, in <module>
gc_all_d.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))
UnicodeEncodeError: 'ascii' codec can't encode character u'\u0329' in position 5: ordinal not in range(128)
在我尝试将咨询师数据库中的一行写入汇总他们姓名的文件后,就会出现错误:
# compile master spreadsheet
with(open('gc_all.txt_3','w')) as gc_all:
gc_all_d = csv.DictWriter(gc_all, fieldnames = fieldnames, extrasaction='ignore', delimiter = '\t')
gc_all_d.writeheader()
for row in aicep_l:
print row['name']
gc_all_d.writerow(row)
for row in nbcc_l:
gc_all_d.writerow(row)
print row['name']
我在这陌生的水域。我在 writerow() 方法中没有看到可以将编码范围扩大到这个字符 '\u0329' 的参数。
我认为该错误可能与我使用名称解析器模块将所有辅导员的姓名组织成相同格式的事实有关。从 nameparser 导入的 HumanName 函数可能会写出带有前导 'u' 的辅导员姓名以表示 unicode,这意味着无法识别总输出 u'Sam the Man' 而不是 'Sam the Man'。
谢谢您的帮助!
根据答案修改后的错误:
File "/private/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup At Startup/merge-395782963.700.py", line 153, in <module>
row['name'] = row['name'].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 11: ordinal not in range(128)
使所有名称条目统一的代码:
# nbcc
with(open('/Users/samuelfinegold/Documents/noodle/gc/nbcc/nbcc_output.txt', 'rU')) as nbcc:
nbcc_d = csv.DictReader(nbcc, delimiter = '\t')
nbcc_l = []
for row in nbcc_d:
# name = HumanName(row['name'])
# row['name'] = name.title + ' ' + name.first + ' ' + name.middle + ' ' + name.last + ' ' + name.suffix
row['phone'] = row['phone'].translate(None, whitespace + punctuation)
nbcc_l.append(row)
修改后的代码:
# compile master spreadsheet
with(open('gc_all.txt_3','w')) as gc_all:
gc_all_d = csv.DictWriter(gc_all, fieldnames = fieldnames, extrasaction='ignore', delimiter = '\t')
gc_all_d.writeheader()
for row in nbcc_l:
row['name'] = row['name'].encode('utf-8')
gc_all_d.writerow(row)
错误:
Traceback (most recent call last):
File "/private/var/folders/jv/9_sy0bn10mbdft1bk9t14qz40000gn/T/Cleanup At Startup/merge-395784700.086.py", line 153, in <module>
row['name'] = row['name'].encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 11: ordinal not in range(128)
logout