2017-01-08 3 views
0

現在、私のJSONデータは次のようにフォーマットされます。出力の新しい行にJSONオブジェクトのグループの代わりに、単一ライン

{"1": {"name": "camera", "aisle": "M.53", "status": "Out of Stock"}, "2": {"name": "camera", "aisle": "M.36", "status": "In Stock"}, "3": {"name": "camera", "aisle": "M.38", "status": "In Stock"}} 

それは各「グループ」を印刷しているので、私は、データの「ブロック」を再フォーマットしたいと思いますのデータはオンラインになっています。データはJSON形式のままにする必要はありません - 私は単純に(次のような)個々の行に情報を分割したい:

result = json.loads(data['searchResults'])['results'][0] 
     summary = { 
      'name': result['name'], 
      'aisle': result['price']['aisle'][0], 
      'status': result['inventory']['status'], 
      } 
     results[store] = summary 

with open('Testing.txt', 'w') as outfile: 
    outfile.write('\n') 
    json.dump(results, outfile) 
を:ここで

"1": {"name": "camera", "aisle": "M.53", "status": "Out of Stock"}, 
"2": {"name": "camera", "aisle": "M.36", "status": "In Stock"}, 
"3": {"name": "camera", "aisle": "M.38", "status": "In Stock"} 

は、私が使用しているコードです。

改行の追加について推奨する方法は何ですか?

+0

フォーマットが重要な場合は、自分で実装するか、より多くの設定でサードパーティのモジュールを探す必要があるかもしれません - 標準ライブラリ 'json'モジュールは、既に見た。 – jonrsharpe

+0

私はまた、ここに投稿されたものを試しましたhttp://stackoverflow.com/questions/21589040/output-group-of-json-objects-on-new-line-instead-of-single-lineしかし、あまり持っていない私のコードで運がいい。 –

+0

[Parse json data in python]の重複している可能性があります。(http://stackoverflow.com/questions/41537153/parse-json-data-in-python) – jonrsharpe

答えて

-1

このanswerで説明したように、PrettyPrintというPythonを使用できます。 これが役に立ちますようお願いいたします。

編集: お詫び申し上げます。 私は、次のテストコードを使用して、所望の出力を達成:

import json 

#example json from your question as text 
before_parsed = '{"1": {"name": "camera", "aisle": "M.53", "status": "Out of Stock"}, "2": {"name": "camera", "aisle": "M.36", "status": "In Stock"}, "3": {"name": "camera", "aisle": "M.38", "status": "In Stock"}}' 

#parsing the text to get the results as a json object 
results = json.loads(before_parsed) 

#print (results) 

#############copy the code from here to your program################ 
#sorting your json output by keys so that you get your output as {"1":{}, "2":{}...} 
results_str = json.dumps(results, sort_keys=True) 

#removing the outer brackets {}, now the results_str would look something like this "1":{}, "2":{}... 
results_str = results_str[1:-1] 

#splitting the string by "}," as delimiter, so results_list would look like this ["1":{, "2":{, "3":{}] 
results_list = results_str.split("},") 

#print the results to the file as per the desired format 
with open('Testing.txt', 'w') as outfile: 
    outfile.write('\n') 
    for p in results_list[:-1]: 
     print (p+'}', file=outfile) 
    print (results_list[-1], file=outfile) 

そして次はTesting.txtファイルに出力されます。

"1": {"aisle": "M.53", "name": "camera", "status": "Out of Stock"} 
"2": {"aisle": "M.36", "name": "camera", "status": "In Stock"} 
"3": {"aisle": "M.38", "name": "camera", "status": "In Stock"} 

は、それはあなたかのために動作しない場合、私に教えてくださいこれはあなたが探しているものではない場合。 乾杯!

+0

あなたの答えの周りにいくつかの文脈を追加してください。リンクのみの回答は理解しにくく、ウェブサイトがダウンしているか機能していないと無効になる可能性があります。それはあなたがこの記事自体に言及しているリンク/ブログの最も関連性の高い部分を再現することができる場合は、アスカーと将来の読者に役立ちます。 – RBT

+0

私は参照された答えを見てきましたが、依然として適用方法は不明です。正しい方向に私を連れて行くためのガイダンスをもうけることができますか? –

+0

ありがとうございます - それは動作します! –

関連する問題