简体   繁体   中英

Error Encoding non-BMP characters

I've developed a little program in python 3.4, but when I try to run it, at the end says:
File "C:\\Python34\\lib\\idlelib\\PyShell.py", line 1352, in write return self.shell.write(s, self.tags) UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 39559-39559: Non-BMP character not supported in Tk

I've tried all, but I found nothing. Help, please!

I presume you did the equivalent of the following.

>>> print('\U00011111')
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    print('\U00011111')
  File "C:\Programs\Python34\lib\idlelib\PyShell.py", line 1347, in write
    return self.shell.write(s, self.tags)
UnicodeEncodeError: 'UCS-2' codec can't encode character '\U00011111' in position 0: Non-BMP character not supported in Tk

The problem is as stated: Idle uses the tkinter interface to tcl/tk and tk cannot display non-BMP supplementary chars (ord(char) > 0xFFFF).

Saving a string with non-BMP chars to a file will work fine as long as you encode with utf-8 (or -16, or -32).

On Windows, the console interpreter gives the same error with 'UCS-2' replaced by 'charmap'. The console interpreter is actually worse in that it raises an error even for some BMP chars, depending on the code page being used. I do not know what the situation is on other systems.

EDIT I forget the best alternative, at least on Windows. Either of the following will print any string on any ascii terminal.

>>> repr('\U00011111')
"'\U00011111'"
>>> ascii('\U00011111')
"'\\U00011111'"

repr() does not double backslashes when echoed, ascii() does. These escape more chars than needed for Idle, but will not raise an exception at the >>> prompt. However, for reasons I do not understand, print(repr('\\U00011111')) fails, so print(ascii(s)) is needed within a program to print s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM