解析json中的引号

时间:2016-08-16 05:19:11

标签: python json

我在json中解析引号时遇到问题。我使用python 2.7。 我的json文件在这里。

{
    "table": "test",
    "rows":
    [
        {
            "comment_id" : "11111",
            "title" : "Worked great with limited space",
            "comment" : "We have a very "small" kitchen but wanted a stylish refrigerator that was counter depth. This model made great use of the limited space. The deep door shelves are great and the touch controls on the door make it easy."
        },
        {
            "comment_id" : "22222",
            "title" : "Amazing Refrigerator",
            "comment" : "Customer Service was "FANTASTIC" when I was shopping for this refrigerator. This refrigerator fit perfectly in our space, it only took 2 hours to cool from delivery. Has a ton of space and the lighting is great in it"
        }
    ]
}

我的来源是:

def create_file(from_file, to_file):
    with open(from_file, "r") as f:
        result = f.read().replace('\\', '').replace('&amp;', '&').replace('&gt;', '>').replace('&lt;', '<')
        res = json.loads(result, strict=False, encoding="ISO-8859-1")

    f = open(to_file, "w")
    f.write("id" + '\t' + "title" + '\t' + "review" + '\n') # write a first line.
    pattern = re.compile("[^a-zA-Z0-9_.;:,!?&]")

    for data in res['rows']:
        output = "\""
        output += str(data['comment_id'] + '"\t"')
        output += str(pattern.sub(' ', data['title']) + '"\t"')
        output += str(pattern.sub(' ', data['comment']) + '""\n')
        f.write(output)
    f.close()

错误代码在这里:

ValueError: Expecting , delimiter: line 20119 column 154 (char 1495987)
Process finished with exit code 1

如果引号(“”)包含在json的注释字段中。 发生错误,我该如何解决?

0 个答案:

没有答案