Python - Encode error - cp850.py

2019-08-15 08:04发布

问题:

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.

回答1:

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.