【发布时间】:2011-02-11 01:27:10
【问题描述】:
我正在尝试编写从某些源代码中提取的一些数据(使用 httplib2 和 lxml.html),但每当我尝试编写数据时,我都会收到此错误:
UnicodeEncodeError: 'charmap' codec can't encode character '\u012b' in position 505: character maps to <undefined>
在整个程序中,我可以很好地打印文本,但是当我尝试写入文件时,我得到了错误。例如,在下面的代码中,我可以很好地打印defs,但是当我尝试写入文件时出现上述错误。
print(defs) #Good
f = open(loc+fname+'.txt', 'w')
f.write(defs) #Bad
f.close()
我怎样才能让它写入文件?
【问题讨论】:
标签: python unicode utf-8 python-3.x