python批量修改json文件
import json
import os
path = 'G:\Desktop\要修改文件的目录'
dirs = os.listdir(path)
num_flag = 0
for file in dirs:# 循环读取路径下的文件并筛选输出
if os.path.splitext(file)[1] == ".json":# 筛选csv文件
num_flag = num_flag +1
print(file , end=' ')
# print(os.path.join(path,file))
with open(os.path.join(path,file),'r') as load_f:
load_dict = json.load(load_f)
load_dict['shapes'][0]['label'] = 'lian_mu_dan_gao1'
print(load_dict['shapes'][0]['label'])
with open(os.path.join(path,file),'w') as dump_f:
json.dump(load_dict, dump_f)
if(num_flag == 0):
print('所选文件夹不存在json文件,请重新确认要选择的文件夹')
else:
【python批量修改json文件】print('共{}个json文件'.format(num_flag))
推荐阅读
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则
- Python数据分析(一)(Matplotlib使用)
- Python|Python 9.20