1

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
  • unrelated: str is a builtin name, don't replace it. u'%s' % s is unnecessary if s is a Unicode string. Commented Aug 24, 2015 at 0:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like this answer should produce mojibake. The error mentions charmap (probably something like cp437), not utf-8. Do not hardcode the character encoding of your environment inside the script.
1

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. Where to change File Encodings

Comments

-1

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.

Comments

Your Answer

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.