Python:JSONファイル
JSONファイルから辞書データを読み込み、要素を変更してから書き込む
[code lang=”python” title=”test.json”]
{
"Sashiko":{"aa":"AA","bb":"BB"},
"Nicorun":{"aa":"AA","bb":"BB"},
"Maggeyy":{"aa":"AA","bb":"BB"}
}
[/code]
[code lang=”python” title=”test.py”]
rf = open(‘/root/test/xxxxxx/test.json’, ‘r’) #相対パスなら(‘test.json’, ‘r’)
name_list = json.load(rf)
print(name_list) #内容の確認
name_list[‘Sashiko’][‘aa’] = ‘ZZ’ #要素の変更
wf = open(‘id_list.json’,’w’)
json.dump(name_list, wf) #変更後のデータの書込み
[/code]
0 Comments