【发布时间】:2019-07-02 23:19:37
【问题描述】:
我正在尝试使用 Python json 包将以下字符串读入字典
但是,在子字段“名称”之一下有一个带有嵌套双引号的描述。我的 json 无法以这种方式读取字符串
import json
string1 =
'{"id":17033,"project_id":17033,"state":"active","state_changed_at":1488054590,"name":"a.k.a.:\xa0"The Sunshine Makers""'
json.loads(string1)
引发了一个错误
JSONDecodeError: Expecting ',' delimiter: line 1 column 96 (char 95)
我知道这个错误的原因是由于 "The Sunshine Makers" 周围的嵌套双引号
如何去掉这个双引号?
更多导致错误的字符串示例
string2 = '{"id":960066,"project_id":960066,"state":"active","state_changed_at":1502049940,"name":"New J. Lye Album - Behind The Lyes","blurb":"I am working on my new project titled "Behind The Lyes" which is coming out fall of 2017."'
#The problem with this string comes from the nested double quote around the pharse "Behind The Lyes inside" the 'blurb' subfield
【问题讨论】:
-
您的字符串也缺少结束
}。确保问题得到解决。 -
如果您希望保留引用,您可以通过将
json.dumps()与json.loads()嵌套来解决此问题:print(json.loads(json.dumps(string1))) -
string1 来自哪里?这是您在代码中创建的东西,还是从网站或数据库中获取的东西?
-
另外,您的错误中的
line 1, column 96 (char 95)是\xa0。 -
垃圾进,垃圾出。修复输入,而不是试图找出如何解决脚本中的错误。
标签: python json regex double-quotes