<返回更多

python读写json文件

2022-11-21  CSDN  
加入收藏

json文件

"name": "Tom","age": 18,"score": {"math": ["192.168.0.1","192.168.0.2","192.168.0.3"],"chinese": 100

Python/ target=_blank class=infotextkey>Python读写代码:

import jsondef read_json(json_path):with open(json_path, 'r', encoding='utf-8') as f:data = json.load(f)print (data)print (type(data))print (data['name'])def write_json(json_path):test = {"name": "Tom","age": 18,"score":"math": ["192.168.0.1","192.168.0.2","192.168.0.3"],"chinese": 100with open("config.json", 'w', encoding='utf-8') as f:json.dump(test, f, indent=4, ensure_ascii=False)if __name__ == "__main__":json_path = "config.json"read_json(json_path)write_json(json_path)

测试结果(目前项目上都是linux系统,使用读写json文件代替读写ini配置文件):

声明:本站部分内容来自互联网,如有版权侵犯或其他问题请与我们联系,我们将立即删除或处理。
▍相关推荐
更多资讯 >>>