' UCS-2'编解码器在解码时不能对字符进行编码

时间:2016-01-23 04:43:25

标签: python

代码:

try:
        json_data = urllib.request.Request(query)
        print(type(json_data))
        with urllib.request.urlopen(json_data) as data:
            data1 = str(data.read().decode('utf-8'))
        print(type(data1))
except Exception as e:
    print ("! Sorry, something went wrong:")
    print ("! Error: %s"%e)
    sys.exit(1)

statuses = json.loads(data1).get("statuses")
texts = [tweet.get("text") for tweet in statuses]

for text in texts:
print (text,"\n")

错误打印:

   'UCS-2' codec can't encode characters in position 227-227: 
      Non-BMP character not supported in Tk.

有没有办法忽略错误并继续或用替换字符替换不支持的代码?

提前致谢。

0 个答案:

没有答案