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.