1. 程式人生 > >'UCS-2' codec can't encode characters in position

'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.

也就是說,開啟的文件中含有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!