2017-02-23 5 views
0

私はPythonには新しく、python_diction(より大きいファイルにある)という辞書の内容を取り出し、そのデータをpython_diction_saved.jsonという新しい新しいファイルに保存しようとしています。私は現在私が受け取っているエラーをかなり閉じ込めているように感じます。python_diction_saved.jsonは定義されていません。辞書にファイルを保存する

ご協力いただければ幸いです。

import json 
f = open("python_diction.txt","w") 
python_diction = {} 
python_diction["items"] = [] 
python_diction["items"].append({"hello":1,"hi":2}) 
python_diction["items"].append({"hello":42,"hi":65}) 
python_diction["numbers"] = [1,2,3,5,7,8] 
f.write(json.dumps(python_diction_saved.json)) 
f.close() 
+0

'python_diction_saved' =>' python_diction' –

+0

グレート、最初のトレースバックエラーを解決しましたが、もう1つのトレースバックエラーが発生しました...... f.write(json.dumps(python_diction_saved.json)) AttributeError: 'dict'オブジェクトに 'json'属性がありません – AvSmith

+0

: 'python_diction_saved.json' =>' python_diction' –

答えて

1

python_diction_saved.jsonという名前のファイルにあなたのpython_dictionを書くために、あなたは(それを自分で書くことを避けるために)json.dump使用することができます。

import json 
python_diction = {} 
python_diction["items"] = [] 
python_diction["items"].append({"hello":1,"hi":2}) 
python_diction["items"].append({"hello":42,"hi":65}) 
python_diction["numbers"] = [1,2,3,5,7,8] 

with open("python_diction_saved.json") as output_file: 
    json.dump(python_diction, output_file) 
関連する問題