2

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 '\u012b' 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?

asked Feb 11, 2011 at 1:27

1 Answer 1

4

Specify an encoding that can handle the character:

f = open(loc + fname + '.txt', 'w', encoding='utf-8')
answered Feb 11, 2011 at 1:56
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.