Cannot print unicode string

China☆狼群 提交于 2021-02-17 03:32:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!