批量修改文件中的 json 内容
作为 python 初学者,你想编写一段代码,能够批量修改路径下的指定 json 文件的内容。例如,你想将 {filename: 1.dav} 这样的 json 内容修改为 {filename: 1.avi}。
代码实现
以下代码可以实现你的要求:
立即学习“Python免费学习笔记(深入)”;
import json # 获取路径 path = u'D:' # 批量获取指定文件名下的 JSON 文件 for file in os.listdir(path): if file.endswith('.json'): # 读取文件内容 with open(os.path.join(path, file), 'r') as f: json_data = json.load(f) # 修改 JSON 内容 json_data['filename'] = '2.avi' # 保存修改后的内容 with open(os.path.join(path, file), 'w') as f: json.dump(json_data, f)
使用说明
重要提示
以上就是如何用 Python 批量修改 JSON 文件内容?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号