2016-08-16 6 views
-1

jsonで引用符の解析に問題がありました。私はPython 2.7を使用します。 私のjsonファイルはこちらです。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でのコメントフィールドに含まれている場合。 エラーが発生しました。どうすれば修正できますか?

+6

あなたのJSONファイルが有効であるを使用して、このようなものでなければなりませんか?埋め込まれた二重引用符はエスケープされていません。 –

+1

あなたのjsonを検証するためにhttp://jsonlint.com/を試してください... –

+0

この特定の部分がそれを破壊している可能性があります:.replace( '\\'、 '') '。なぜあなたはこれをする必要がありますか? –

答えて

0

あなたのJSONが有効ではありません、それは:-) が有効な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" 
    }] 
} 
関連する問題