0
JSONファイルをインポートし、それをテンプレートとして使用していくつかのプロパティを変更しています。テンプレートとしてのJSON:クリアされません。
with open('example.json') as data_file:
j = json.load(data_file)
... # and within a loop:
with open('output.json', 'w') as outfile:
activejson = j # I would like this to reset or pull fresh from j
... # modify properties here
json.dump(activejson, outfile, indent=4)
activejson
ループの各反復を介してテンプレートj
新鮮を引いていません。 (私は、以前の反復の古い値を、ここのプロパティを変更する "activejson
"の以前の反復から引き出すことができます)。activejson
はj
を指していますが、activejson = j.copy()
はそのトリックどちらも、そうではありませんactivejson = {}
。この変数を空にし、各反復後にテンプレートを新鮮にする正しいコマンド/方法は何ですか?これを処理する最も効率的な方法は何ですか?
'j.copy()'は浅いコピーのみを扱うので、ネストされたオブジェクトにはまだ問題があります。 [copy.deepcopy](https://docs.python.org/3/library/copy.html#copy.deepcopy)を参照してください。 –