Python 修改json file

发布时间 2023-03-22 21:08:51作者: 筱筱的春天

 

import json def print_hi(name):
    print(f'Hi, {name}') if __name__ == '__main__':
    print_hi('PyCharm') updateValue = 12345
with open('jsonfile/create_mspc_bettypes.json') as file:
    json_data = json.load(file)
    for key, value in json_data.items():
        # print(f'{key}={value}')
        if key == 'eventID':
            json_data['eventID'] = updateValue
            with open('jsonfile/create_mspc_bettypes.json', "w") as file2:
                json.dump(json_data, file2, ensure_ascii=False, indent = 2)
                # file2.write(json.dumps(json_data))
            file2.close()
file.close()