python json.loads失败怎么办
json.loadsPython错误:
1、'utf8'codeccan'tdecodebyte...
由于需求,要用python读取网页返回json,并取得其中的数据但是却遇到以上编码的问题。
终于找到了解决方案:
我们只需要对字符串进行unicode化,忽略错误就行了。
代码如下:
str2=unicode(str,errors='ignore')
但是要注意,这里的str必须为字符串,处理之后再送入json.load就可以了。
data=json.loads(str2)
unicode化+忽略错误真给力。
2、ValueError:Invalid\escape:line1column35442(char35441)
问题在于编码中是\xa0之类的,当遇到有些不用转义的\http之类的,则会出现以上错误。
解决方案如下:
importre
regex=re.compile(r'\\(?![/u"])')
fixed=regex.sub(r"\\\\",original)
以上内容为大家介绍了python培训之json.loads失败怎么办,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python求和函数sum()详解
今天在学习的过程中,误用sum()函数,我又去查了查pythonsum()函数才恍然大悟。我本来想算几个Int值相加的和,本以为很简单的事情,结果却很悲...详情>>
2023-11-09 23:36:32
python子类可以调用父类方法吗
python中的子类中的__init__()函数会覆盖父类的函数,一些情况往往需要在子类里调用父类函数。如下例程里,处是需要调用父类函数的地方,接下来...详情>>
2023-11-09 20:20:51
python json.loads失败怎么办
json.loadsPython错误:1、'utf8'codeccan'tdecodebyte...由于需求,要用python读取网页返回json,并取得其中的数据但是却遇到以...详情>>
2023-11-09 16:41:05
python如何安装第三方函数库
python安装第三方库有两种方式:1.使用pip命令行工具在线下载你需要的第三方库2.手动下载第三方库,再使用pip命令安装1.使用pip命令行工具在线...详情>>
2023-11-09 16:01:37