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?

BartoszKP
  • 32,105
  • 13
  • 92
  • 123
user3544092
  • 323
  • 1
  • 4
  • 12
  • unrelated: `str` is a builtin name, don't replace it. `u'%s' % s` is unnecessary if `s` is a Unicode string. – jfs Aug 24 '15 at 00:55

3 Answers3

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.

PYPL
  • 1,660
  • 1
  • 21
  • 36
  • 1
    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. – jfs Aug 24 '15 at 01:00
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

Darkhan ZD
  • 389
  • 7
  • 10
-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.

Community
  • 1
  • 1
jfs
  • 346,887
  • 152
  • 868
  • 1,518