Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ';UCS-2和x27;编解码器可以';解码时不编码字符_Python - Fatal编程技术网

Python ';UCS-2和x27;编解码器可以';解码时不编码字符

Python ';UCS-2和x27;编解码器可以';解码时不编码字符,python,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 a

代码:

错误打印:

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")
有没有办法忽略错误并继续,或者用替换字符替换不支持的代码


提前谢谢

。。。为什么要对字节进行解码,然后将其传递给
str()
?我需要使用json.load(),它要求data1是字符串而不是byteb,但为什么要对其进行解码?因为我得到了以下错误:json.decoder.jsondecodecorr:期望值:第1行第1列(char 0),我会编辑主要问题中的代码以获得更清晰的图片
   'UCS-2' codec can't encode characters in position 227-227: 
      Non-BMP character not supported in Tk.