zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python学习笔记 --- python实现修改json文件

2023-09-27 14:27:48 时间

基本原理就是通过读取文件,将json数据读取出来,然后进行修改,再写回文件中。

# -*- coding:utf-8 -*-
import json
def process_json(input_json_file, output_json_file):
    file_in = open(input_json_file, "r")
    file_out = open(output_json_file, "w")
    # load数据到变量json_data
    json_data = json.load(file_in)
    print json_data
    print "after update  --->"
    print type(json_data)
    # 修改json中的数据
    json_data["job"] = "hahah"
    print json_data
    
    # 将修改后的数据写回文件
    file_out.write(json.dumps(json_data))
    file_in.close()
    file_out.close()
 
process_json("../jsonfile/mysql2hive_templet.json","../jsonfile/mysql2hive_instance.json")