2016-04-09 28 views
-2

私はスクリプトが動作する必要がある.jsonファイルを持っています。問題は、私のスクリプトが入力ファイルを移動するためにlinereadersを使用していることと、私が持っているjsonファイルがすべて1つの巨大な1行にあることです。ファイルを見ると、新しい行を作成する場所を見つけるために何ができるのかよく分かりません。誰が私がこれをどうやってやるかも知っていますか?私のプロジェクトでは、この.jsonファイル(以下の例の1つ)を使用して、正しくフォーマットするか(または少なくとも新しい行で)作成し、スクリプトの入力としてこれを読むことが不可欠です。事前のおかげで.jsonファイルを読み込んで、新しいファイルに書き込む方法、Pythonで改行を挿入する

例1は、現在の方法は、.jsonファイルが見えます:

{"nodes":[{"nodeID":"119927","text":"Yes, it's annoying and cumbersome to separate your rubbish properly all the time.","type":"I","timestamp":"2015-12-14 12:09:13"},{"nodeID":"119928","text":"Three different bin bags stink away in the kitchen and have to be sorted into different wheelie bins.","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119929","text":"But still Germany produces way too much rubbish","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119930","text":"and too many resources are lost when what actually should be separated and recycled is burnt.","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119931","text":"We Berliners should take the chance and become pioneers in waste separation!","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119932","text":"Default Conflict","type":"CA","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119933","text":"Default Inference","type":"RA","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119934","text":"Default Conflict","type":"CA","timestamp":"2015-12-14 12:09:14"}],"edges":[{"edgeID":"160906","fromID":"119927","toID":"119932","formEdgeID":null},{"edgeID":"160907","fromID":"119932","toID":"119931","formEdgeID":null},{"edgeID":"160908","fromID":"119928","toID":"119933","formEdgeID":null},{"edgeID":"160909","fromID":"119933","toID":"119927","formEdgeID":null},{"edgeID":"160910","fromID":"119929","toID":"119934","formEdgeID":null},{"edgeID":"160911","fromID":"119934","toID":"119932","formEdgeID":null},{"edgeID":"160912","fromID":"119930","toID":"119934","formEdgeID":null}],"locutions":[]} 

例2、私のスクリプトがで正常に動作フォーマット:大幅に高く評価されて

{ 
    "nodes":[ 
     { 
      "nodeID":20, 
      "text":"Yes, it's annoying and cumbersome to separate your rubbish properly all the time.", 
      "type":"I" 
     }, 
     { 
      "nodeID":21, 
      "text":"Three different bin bags stink away in the kitchen and have to be sorted into different wheelie bins.", 
      "type":"I" 
     }, 
     { 
      "nodeID":22, 
      "text":"But still Germany produces way too much rubbish", 
      "type":"I" 
     }, 
     { 
      "nodeID":23, 
      "text":"and too many resources are lost when what actually should be separated and recycled is burnt.", 
      "type":"I" 
     }, 
     { 
      "nodeID":24, 
      "text":"We Berliners should take the chance and become pioneers in waste separation!", 
      "type":"I" 
     }, 
     { 
      "nodeID":40, 
      "text":"Default Conflict", 
      "type":"CA" 
     }, 
     { 
      "nodeID":41, 
      "text":"Default Inference", 
      "type":"RA" 
     }, 
     { 
      "nodeID":42, 
      "text":"Default Conflict", 
      "type":"CA" 
     } 
    ], 
    "edges":[ 
     { 
      "fromID":20, 
      "toID":40 
     }, 
     { 
      "fromID":40, 
      "toID":24 
     }, 
     { 
      "fromID":21, 
      "toID":41 
     }, 
     { 
      "fromID":41, 
      "toID":20 
     }, 
     { 
      "fromID":22, 
      "toID":42 
     }, 
     { 
      "fromID":42, 
      "toID":40 
     }, 
     { 
      "fromID":23, 
      "toID":42 
     } 
    ], 
    "schemefulfillments":[ 

    ], 
    "participants":[ 

    ], 
    "locutions":[ 

    ] 
} 

すべてのヘルプ。私は私のプログラム全体を完全に書き換えるプロジェクトが終わる前に時間がないので、元のスクリプトに大幅な書き換えを必要としないこのようなやり方が必要です。

+0

あなたが現在ので、我々はすべての問題を見つけることができます使用しているコードを投稿してください。 –

+0

有効なJSONにはその書式設定は必要ありません。多分あなたは自分のものを動かすのではなく、適切なパーサを使うべきでしょうか? – jonrsharpe

+3

あなたのスクリプトが有効なJSON上で綺麗に印刷されていないものがあれば、何かが**非常に間違っていますので、事前印刷する代わりに修正する必要があります。スクリプトでJSONパーサーを使用してJSONを解析しないのはなぜですか? JSONを解析できないJSONパーサーを使用していると、おそらく失敗します; – ThiefMaster

答えて

1

これを試してください。

入力:

import json 

compactJSON = """{"nodes":[{"nodeID":"119927","text":"Yes, it's annoying and cumbersome to separate your rubbish properly all the time.","type":"I","timestamp":"2015-12-14 12:09:13"},{"nodeID":"119928","text":"Three different bin bags stink away in the kitchen and have to be sorted into different wheelie bins.","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119929","text":"But still Germany produces way too much rubbish","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119930","text":"and too many resources are lost when what actually should be separated and recycled is burnt.","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119931","text":"We Berliners should take the chance and become pioneers in waste separation!","type":"I","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119932","text":"Default Conflict","type":"CA","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119933","text":"Default Inference","type":"RA","timestamp":"2015-12-14 12:09:14"},{"nodeID":"119934","text":"Default Conflict","type":"CA","timestamp":"2015-12-14 12:09:14"}],"edges":[{"edgeID":"160906","fromID":"119927","toID":"119932","formEdgeID":null},{"edgeID":"160907","fromID":"119932","toID":"119931","formEdgeID":null},{"edgeID":"160908","fromID":"119928","toID":"119933","formEdgeID":null},{"edgeID":"160909","fromID":"119933","toID":"119927","formEdgeID":null},{"edgeID":"160910","fromID":"119929","toID":"119934","formEdgeID":null},{"edgeID":"160911","fromID":"119934","toID":"119932","formEdgeID":null},{"edgeID":"160912","fromID":"119930","toID":"119934","formEdgeID":null}],"locutions":[]}""" 

dictionary = json.loads(compactJSON) 

prettyJSON = json.dumps(dictionary, indent=4, separators=(',', ': ')) 

print(prettyJSON) 

出力:

{ 
    "edges": [ 
     { 
      "fromID": "119927", 
      "edgeID": "160906", 
      "formEdgeID": null, 
      "toID": "119932" 
     }, 
     { 
      "fromID": "119932", 
      "edgeID": "160907", 
      "formEdgeID": null, 
      "toID": "119931" 
     }, 
     { 
      "fromID": "119928", 
      "edgeID": "160908", 
      "formEdgeID": null, 
      "toID": "119933" 
     }, 
     { 
      "fromID": "119933", 
      "edgeID": "160909", 
      "formEdgeID": null, 
      "toID": "119927" 
     }, 
     { 
      "fromID": "119929", 
      "edgeID": "160910", 
      "formEdgeID": null, 
      "toID": "119934" 
     }, 
     { 
      "fromID": "119934", 
      "edgeID": "160911", 
      "formEdgeID": null, 
      "toID": "119932" 
     }, 
     { 
      "fromID": "119930", 
      "edgeID": "160912", 
      "formEdgeID": null, 
      "toID": "119934" 
     } 
    ], 
    "locutions": [], 
    "nodes": [ 
     { 
      "timestamp": "2015-12-14 12:09:13", 
      "nodeID": "119927", 
      "text": "Yes, it's annoying and cumbersome to separate your rubbish properly all the time.", 
      "type": "I" 
     }, 
     { 
      "timestamp": "2015-12-14 12:09:14", 
      "nodeID": "119928", 
      "text": "Three different bin bags stink away in the kitchen and have to be sorted into different wheelie bins.", 
      "type": "I" 
     }, 
     { 
      "timestamp": "2015-12-14 12:09:14", 
      "nodeID": "119929", 
      "text": "But still Germany produces way too much rubbish", 
      "type": "I" 
     }, 
     { 
      "timestamp": "2015-12-14 12:09:14", 
      "nodeID": "119930", 
      "text": "and too many resources are lost when what actually should be separated and recycled is burnt.", 
      "type": "I" 
     }, 
     { 
      "timestamp": "2015-12-14 12:09:14", 
      "nodeID": "119931", 
      "text": "We Berliners should take the chance and become pioneers in waste separation!", 
      "type": "I" 
     }, 
     { 
      "timestamp": "2015-12-14 12:09:14", 
      "nodeID": "119932", 
      "text": "Default Conflict", 
      "type": "CA" 
     }, 
     { 
      "timestamp": "2015-12-14 12:09:14", 
      "nodeID": "119933", 
      "text": "Default Inference", 
      "type": "RA" 
     }, 
     { 
      "timestamp": "2015-12-14 12:09:14", 
      "nodeID": "119934", 
      "text": "Default Conflict", 
      "type": "CA" 
     } 
    ] 
} 
関連する問題