当前位置:网站首页>json按格式逐行输出到文件
json按格式逐行输出到文件
2022-07-22 10:28:00 【cyz0202】
Dict/Json输出文件中逐行显示
- 需要的效果如下图,我有一个list of dicts,它在json.cn中显示如下,我需要它输出到文件也显示同样的格式,而非整个list是一行:
- 方法:
# 写入json对象
import json
dataset = [{
"a":1, "b":2, "c":3}, {
"d":[1,2,3], "e":[2,3,4], "f":[3,4,5]}]
with open("your_file_path", "w", encoding="utf8") as wo:
json.dump(dataset, wo, ensure_ascii=False, indent=4)
# 读取json对象
...
with open("your_file_path", encoding="utf8") as ro:
dataset = json.load(ro) # 得到list of dicts
- json.dump()把dataset写到wo文件流中,ensure_ascii=False确保中文字符编码正确,indent>0设置各行的相对缩进值;最后文件中的显示效果大致就如1中的图;
- 如果要读取3中文件的对象,可以直接json.load(ro),如上代码所示;
边栏推荐
猜你喜欢
Altium一键自动出BOM
What should I do if the web page is hijacked? How to repair DNS hijacked? Introduction to web hijacking
Simple implementation process of CNN
信号降噪方法
CPU亲和力
Websites jump inexplicably. What is website hijacking from Baidu? How to solve Baidu snapshot hijacking
信号量实现同步互斥经典案例
机器学习入门:逻辑回归-2
Microblaze添加自定义IP核,挂AXI总线实现SSD1306 OELD驱动
Elastic Search 学习入门之核心概念(四)
随机推荐
What does Baidu snapshot hijacking mean? How to solve Baidu snapshot hijacking and Baidu hijacking
DNS劫持如何预防、DNS是什么?DNS劫持详解
Getting started with elastic search: installation and configuration of elastic search (I)
笔记:C语言
出现Permission denied的解决办法
常见的网站被黑被劫持的手段有哪些?dns劫持工具有那些?
JNI 数据类型用法
Nc88 looking for the K largest
蓝桥杯——进制转换练习
Host Ping fails in VMware NAT mode virtual machine: different from most methods
机器学习入门:线性回归-1
Usage and precautions of accumulator used in spark
When using CV2 to realize face recognition, Chinese is displayed on the recognition frame
win8.1系统发生蓝屏问题解决方法、IE主页被恶意劫持怎么解决?
Redis accesses JSON data
redis集群搭建
What is the phenomenon of DNS being hijacked? What is DNS? How to solve DNS hijacking
Flutter development (30): simple app building of flutter
The setting of node.master and node.data in the production environment of the introduction to elastic search (3)
网页被劫持了该怎么办?dns被劫持如何修复?网页劫持介绍