python3.8 修改/增加json文件中的中文字段

python3.8 修改/增加json文件中的中文字段

# encoding: utf-8
import json

# 读取配置
def read_config():
    with open("jsonfile/file.json") as json_file:
        config = json.load(json_file,encoding='gbk')
        print('read_config:',config)
    return config

# 从参数更新配置
def update_config(config):
    print('update_config:',config)
    with open("jsonfile/file.json", 'w') as json_file:
        json.dump(config, json_file, indent=4, ensure_ascii=False)
    return None

# 把配置从配置文件读取到变量
config = read_config()
print('config:',config)

def execute():
    print('execute:',config)
    pass

# 从全局变量更新配置
def save_config():
    update_config(config)
    print('save_config:',config)

execute() # 打印配置值
if "疾病名称1" not in config:   # 如果config里没有"疾病名称1"
    config['疾病名称1'] = {}  # 增加配置值,config["疾病名称1"]
execute()  # 打印配置值,配置值已修改
save_config() # 执行保存方法,把更新后的值保存到配置文件
execute()
print('last',config)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值