python-辞書と秩序のJSONファイルを書き込みます

一般的なプロセス:

  1. ガイドパッケージ---インポートコレクション
  2. 注文した辞書を作成--- collections.OrderedDict()
  3. JSONファイルを書きます
コード:
1つの インポートコレクション
 2 REAL_RESULT = collections.OrderedDict()
 3 REAL_RESULT [ " ターゲット" ] = " total_result " 
4 REAL_RESULT [ " KEY1 " ] = " 値1 " 
5 REAL_RESULT [ " KEY2 " ] = " value2の" 
6 REAL_RESULT [ " KEY3 " ] = " 値3 "
7 real_result_total = [REAL_RESULT]
 8  プリントreal_result_total
 9  
10输出:[OrderedDict([(' 標的'' total_result ')、(' KEY1 '' 値1 ')、(' KEY2 '' 値2 ')、(' KEY3 '' VALUE3 ' )]) ]
 11  
12 current_dir = os.path.dirname(os.path.realpath(__FILE__ ))
 13パス= os.path.join(os.path.join(current_dir、'jsonfile ")、result_json)
 14オープン(経路と、' W ' Fとして):
 15      json.dump(real_result_total、F、エンコード= " UTF-8 "、ensure_ascii = Falseを、インデント= 4、セパレータ=(' '' "))

JSONファイル:

 

おすすめ

転載: www.cnblogs.com/pythonRoad/p/11612022.html