python 读入修改写入json文件

前言

宁可花点时间写个效率工具,也不要为了图一时方便重复做恶心自己的没有意义的活…

代码

import json
import os, sys
def update_json_data(brick_name, data_dict):
	json_names = [需要查的文件名]
	goal_path = ''
	new_json_data = None
	for name in json_names:
		json_path = 'User/xxx/Downloads/xxx/{}.json'.format(name)
		with open(json_path, 'rb') as f:
			json_data = json.load(f)
			for k,v in json_data.items():
				if k == brick_name:
					v.update(data_dict)
					goal_path = json_path
					new_json_data = json_data
					break
		f.close()
		if goal_path:
			break
	with open(goal_path, 'w') as r:
		json.dump(new_json_data, r, indent = 4)
	r.close()

你可能感兴趣的:(python)