json.loadsPython錯(cuò)誤:
1、'utf8'codeccan'tdecodebyte...
由于需求,要用python讀取網(wǎng)頁(yè)返回json,并取得其中的數(shù)據(jù)但是卻遇到以上編碼的問(wèn)題。
終于找到了解決方案:
我們只需要對(duì)字符串進(jìn)行unicode化,忽略錯(cuò)誤就行了。
代碼如下:
str2=unicode(str,errors='ignore')
但是要注意,這里的str必須為字符串,處理之后再送入json.load就可以了。
data=json.loads(str2)
unicode化+忽略錯(cuò)誤真給力。
2、ValueError:Invalid\escape:line1column35442(char35441)
問(wèn)題在于編碼中是\xa0之類(lèi)的,當(dāng)遇到有些不用轉(zhuǎn)義的\http之類(lèi)的,則會(huì)出現(xiàn)以上錯(cuò)誤。
解決方案如下:
importre
regex=re.compile(r'\\(?![/u"])')
fixed=regex.sub(r"\\\\",original)
以上內(nèi)容為大家介紹了python培訓(xùn)之json.loads失敗怎么辦,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。