Python 3.2 UnicodeEncodeE

2024-04-25 01:09:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图写一些从源代码中提取的数据(使用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()

我怎样才能把这个写到文件里?在


Tags: 文件数据源代码html错误canlxmldefs