'UCS-2' codec can't encode characters in position

前几天在写python程序时,遇到了以上问题,在Stackovereflow上找到了解决方案,记录一下:

Your data contains characters outside of the Basic Multilingual Plane. Emoji's for example, are outside the BMP, and the window system used by IDLE, Tk, cannot handle such characters.

You could use a translation table to map everything outside of the BMP to the replacement character:

也就是说,打开的文档中含有BMP基本多文种字面之外的字符,需要以下的代码来包含BMP之外的字符:

import sys
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
print(x.translate(non_bmp_map))
The  non_bmp_map  maps all codepoints outside the BMP (any codepoint higher than 0xFFFF, all the way up to the  highest Unicode codepoint your Python version can handle ) to  U+FFFD REPLACEMENT CHARACTER :

test测试:

>>> print('This works! \U0001F44D')
This works! 

你可能感兴趣的:(python学习)