2017-02-23 8 views
0

저는 파이썬에 관해서 매우 익숙합니다. 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

그레이트, 첫 번째 추적 오류를 해결했지만 지금은 또 다른 추적 오류가 발생했습니다 ...... 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)