如何摆脱在“名称”子嵌套双引号的?

问题描述 投票:0回答:1

我想读下面的字符串转换成字典使用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)

A错误升高

JSONDecodeError: Expecting ',' delimiter: line 1 column 96 (char 95)

我知道,要围绕“阳光庄家”嵌套双引号此错误的原因是由于

如何我摆脱这种双引号的?

导致错误字符串的更多示例

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 
python json regex double-quotes
1个回答
0
投票

请注意,您的字符串有一个以上的问题,使之无效JSON

你看到的错误是\xa0(非打破空间)。这之前,需要先""问题就变成了一个要解决的问题。

你的字符串缺少一个右}

这就是说,对于字符串你先引,一种方法来修复您的问题是使用.replace()

string1 = '{"id":17033,"project_id":17033,"state":"active","state_changed_at":1488054590,"name":"a.k.a.:\xa0"The Sunshine Makers""'.replace('\xa0"', "'").replace('""', "'\"") + '}'

例如,下面的处理在你的两个样本串的双重报价和其他问题:

import json 

fixes = [('\xa0', ' '),('"',"'"),("{'",'{"'),("','", '","'),(",'", ',"'),("':'", '":"'),("':", '":'),("''", '\'\"'), ("'}",'"}')]

print(fixes)
string1 = '{"id":17033,"project_id":17033,"state":"active","state_changed_at":1488054590,"name":"a.k.a.:\xa0"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."'
strings = [string1, string2]

for string in strings:
    print(string)
    string = string + '}'
    for fix in fixes:
        string = string.replace(*fix)
    print(string)
    print(json.loads(string)['name'])

这将是有益的,如果你可以填写与您从中检索这些字符串代码或文件你的问题。这将有可能给出一个比较全面的回答。

© www.soinside.com 2019 - 2024. All rights reserved.