Python 3.2 UnicodeEncodeError
I am trying to write some data pulled from some source code (using httplib2 and lxml.html), but whenever I try to write the data I get this error:
UnicodeEncodeError: 'charmap' codec can't encode character '\u开发者_C百科012b' in position 505: character maps to <undefined>
Throughout the whole program I can print the text just fine, but when I try to write to a file I get the error. For example, in the following code I can print defs
just fine, but I get the above error when I try to write to a file.
print(defs) #Good
f = open(loc+fname+'.txt', 'w')
f.write(defs) #Bad
f.close()
How can I get this to write to the file?
Specify an encoding that can handle the character:
f = open(loc + fname + '.txt', 'w', encoding='utf-8')
精彩评论