1

I am Python beginner so I hope this problem will be an easy fix.

I would like to print the value of an attribute as follows:

print (follower.city)

I receive the following error message:

File “C:\Python34\lib\encodings\cp850.py“, line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: ‘charmap‘ codec can’t encode character ‘\u0130‘ 0: character maps to (undefined)

I think the problem is that cp850.py does not contain the relevant character in the encoding table.

What would be the solution to this problem? No ultimate need to display the character correctly, but the error message must be avoided. Do I need to modify cp850.py?

Sorry if this question has been addressed before, but I was not able to figure it out using previous answers to this topic.

kanimbla
  • 858
  • 1
  • 9
  • 23
  • You are trying to print `utf-8` data, but your console cannot show it . Change the default encoding for your console by doing `chcp 65001` (to utf-8) (not inside python, directly in command prompt) . – Anand S Kumar Aug 04 '15 at 09:28

1 Answers1

4

To print a string it must first be converted from pure Unicode to the byte sequences supported by your output device. This requires an encode to the proper character set, which Python has identified as cp850 - the Windows Console default.

Starting with Python 3.3 you can set the Windows console to use UTF-8 with the following command issued at the command prompt:

chcp 65001

This should fix your issue, as long as you've configured the window to use a font that contains the character.

Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
  • What do you mean starting with Python 3.4 ? Does it not work for prior versions? – Anand S Kumar Aug 04 '15 at 09:31
  • It doesn't work for 2.* versions (still not sure, since Windows). And said 3.4 since OP is using that version. – Parag Tyagi Aug 04 '15 at 09:41
  • Ok, just for completion sake , I am dropping the following link here - https://docs.python.org/dev/whatsnew/3.3.html#codecs - *A new Windows-only codec has been added: cp65001 (issue 13216). It is the Windows code page 65001 (Windows UTF-8, CP_UTF8).* . It is supported from Python 3.3 onwards. – Anand S Kumar Aug 04 '15 at 09:43
  • Cool. So it is supported from Python 3.3 onwards. – Parag Tyagi Aug 04 '15 at 09:45
  • Thanks a lot to both of you! However, typing `chcp 65001` in the cmd terminal only solves the problem temporarily. I am retrieving data through a JSON request - after the first request the print command works fine. Once I make a new request, I receive the same error again when using the print command. Any ideas what is the problem? – kanimbla Aug 04 '15 at 18:05