问题
I'm working with dbf database and Armenian letters, the DBF encoding was unknown so I've created a letter map to decode revived string. Now I have a valid Unicode string, but I cannot print it out because of this error:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to
What I have tried so far:
print u'%s' %str ## Returns mentioned error
print repr(str) ## Returns string in this form u'\u054c\u0561\u0586\u0561\u0575\u0565\u056c
How to fix it?
回答1:
try to do the following:
newStr = str.encode("utf-8")
print newStr
P.S. Had this problem with another language, was able to view letters when wrote them into a file.
回答2:
In my case, I have changed encoding settings in my IDE. I use PyCharm.
Go to: "File -> Settings... -> Editor -> File Encodings" and change everything to needed charset. I hope it will help to someone.
回答3:
To print a valid Unicode string, use print(unicode_string)
.
The error suggests that you are on Windows. To print Unicode on Windows, see this answer -- it is for Python 3.4 but it should work for Python 2.7 with minor modifications.
来源:https://stackoverflow.com/questions/32156703/cannot-print-unicode-string