'UCS-2'编解码器无法编码位置1050-1050中的字符 [英] 'UCS-2' codec can't encode characters in position 1050-1050

查看:476
本文介绍了'UCS-2'编解码器无法编码位置1050-1050中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 文件E:\python343 \\ .py,第31行,< module> 
print(x1)
文件E:\python343\lib\idlelib\PyShell.py,第1347行,写入
return self.shell.write(s, self.tags)
UnicodeEncodeError:'UCS-2'编解码器无法编码位置1050-1050中的字符:Tk
中不支持非BMP字符

这是我的代码:

  x = g.request搜索',{'q':'TaylorSwift','type':'page','limit':100})['data'] [0] ['id'] 

#所有状态在特定页面上(X =页面ID)
for g.get_connections中的x1(x,'feed')['data']:
print(x1)
for x2 in $($)
print(x2)
if(x2 [1] =='status'):
x2 ['message']
pre>

我如何解决这个问题?

解决方案

Basic之外的字符多语言平面 。例如,表情符号在BMP之外,IDLE,Tk使用的窗口系统无法处理这样的字符。



您可以使用翻译表将BMP之外的所有内容映射到替换字符

  import sys 
non_bmp_map = dict.fromkeys(range(0x10000,sys.maxunicode + 1),0xfffd)
print(x.translate(non_bmp_map))

non_bmp_map 映射BMP外的所有代码点(任何代码点高于0xFFFF,一直到您的Python版本可以处理的最高Unicode码点)到 U + FFFD REPLACEMENT CHARACTER

 >>> print('This works!\U0001F44D')
这个工程!

When I run my Python code, I get the following errors:

  File "E:\python343\crawler.py", line 31, in <module>
    print (x1)
  File "E:\python343\lib\idlelib\PyShell.py", line 1347, in write
    return self.shell.write(s, self.tags)
UnicodeEncodeError: 'UCS-2' codec can't encode characters in position 1050-1050: Non-BMP character not supported in Tk

Here is my code:

x = g.request('search', {'q' : 'TaylorSwift', 'type' : 'page', 'limit' : 100})['data'][0]['id']

# GET ALL STATUS POST ON PARTICULAR PAGE(X=PAGE ID)
for x1 in g.get_connections(x, 'feed')['data']:
    print (x1)
    for x2 in x1:
        print (x2)
        if(x2[1]=='status'):
            x2['message']

How can I fix this?

解决方案

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:

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:

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

这篇关于'UCS-2'编解码器无法编码位置1050-1050中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆