python3 之 格式化json
2024-08-22 23:03:23
import json json_string = None with open("json_file.json") as f:
json_string = f.read()
try:
parsed_json = json.loads(json_string)
formatted_json = json.dumps(parsed_json, indent = 4,sort_keys=True)
with open("json_file.json","w") as f:
f.write(formatted_json)
except Exception as e:
print(repr(e))
Parameter indent decides the number of spaces to use for indentation.
Parameter sort_keys
decides whether the keys in formatted JSON should be in sorted order or not. Default value is False.
Bonus: You can pretty-print the small JSON string directly at command terminal without using python script file. Use below line of code.
echo '{"first_name": "Anurag", "last_name": "rana"}' | python -m json.tool
最新文章
- 用idea写servlet文件
- Bootstrap 类解析
- css 中的position z-index em rem zoom 的基本用法
- 《Code Complete》ch.25 代码调整策略
- Linux解压 tar命令
- maven系列(3)-maven生命周期的介绍
- 如何在xcode下面同时安装cocos2d-iphone 和 cocos2d-x模板,其实是因为很喜欢C++的缘故,当时学习的是前者,现在自己摸着石头过河了就(cocos2d-x安装失败 出错)
- IPv4头部结构具体解释
- 有关Struts2a的ction直接使用response异步问题
- Bash函数
- [C#]使用GroupJoin将两个关联的集合进行分组
- 02.将SDK获取到的ECS主机信息入库
- [SCOI 2011]糖果
- ffdshow 源代码分析 9: 编解码器有关类的总结
- C#算法与数据结构之线性结构
- 个人博客搭建( wordpress )
- php中安装memcache
- DevExpress v18.1新版亮点——Office File API篇
- Linux Foundation(笔记)
- vue mock(模拟后台数据) 最简单实例(一)——适合小白
热门文章
- 阿拉伯数字转中文大写数字的JS
- PHP面试题 – 培训学校真实面试内部资料
- 20145230《java程序设计》第五次实验报告
- 8.14比赛j题 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#overview
- Centos6.5下Hbase安装
- SG函数略解
- 【P2564】生日礼物(单调队列)
- Phoenix性能优化
- [kuangbin带你飞]专题十 匹配问题 一般图匹配
- Avoid nesting too deeply and return early避免嵌套太深应尽早返回