主要关注两点:
1. json文件数据加载有序
json_data = json.load(cur_file,object_pairs_hook=collections.OrderedDict)
2. 导出数据缩进格式化:
cur_file.write(json.dumps(json_data,indent=4))
import sys
import os
import json
import collections
def lm_tag_checkout(filename,tag_namespace):
cur_file = open(filename,"r+")
json_data = json.load(cur_file,object_pairs_hook=collections.OrderedDict)
if COMMENT_SWITCH:
print("old tag:",json_data["tag"])
json_data["tag"] = "xxx"+str(tag_namespace)
if COMMENT_SWITCH:
print("new tag:",json_data["tag"])
cur_file.seek(0)
cur_file.truncate()
cur_file.write(json.dumps(json_data,indent=4))
cur_file.close()