当前位置:网站首页>JSON output to file line by line in format
JSON output to file line by line in format
2022-07-22 20:29:00 【cyz0202】
Dict/Json Line by line in the output file
- The desired effect is shown in the figure below , I have one list of dicts, It's in json.cn It shows as follows , I need it to output to the file in the same format , Not the whole list It's a line :
- Method :
# write in json object
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)
# Read json object
...
with open("your_file_path", encoding="utf8") as ro:
dataset = json.load(ro) # obtain list of dicts
- json.dump() hold dataset writes wo In file stream ,ensure_ascii=False Make sure the Chinese character encoding is correct ,indent>0 Set the relative indentation value of each line ; The display effect in the final file is roughly like 1 Map in ;
- If you want to read 3 Objects in files , Can directly json.load(ro), As shown in the above code ;
边栏推荐
- VIM uses tips
- mysql引擎
- Core concepts of elastic search learning Introduction (4)
- Pytorch网络训练流程的作用原理:源码分析optimizer.zero_grad()loss.backward()optimizer.step()
- redission分布式锁种类
- LeetCode21——Merge two sorted lists——Iteration or recursion
- Signal noise reduction method
- Introduction to machine learning: Logistic regression-2
- SSM framework integration
- docker搭建mysql主从复制
猜你喜欢
本地镜像发布到阿里云
LeetCode103——zigzagLevelOrder of binary tree
Reinforcement learning weekly 39: approximate optimal depth, multi-agent generalization, character animation reinforcement learning
mysql引擎
Websites jump inexplicably. What is website hijacking from Baidu? How to solve Baidu snapshot hijacking
docker搭建mysql主从复制
机器学习入门:支持向量机-6
Pytorch不同层设置不同学习率
How to solve the blue screen problem in win8.1 system and how to solve the malicious hijacking of IE home page?
"Pre training weekly" No. 38: transformer, Bert structure optimization
随机推荐
Iptables for load balancing
LeetCode103——zigzagLevelOrder of binary tree
vim配置
1049 Counting Ones (30 分)
How to deal with DNS hijacking, DNS hijacking, and DNS hijacking solutions
1080 Graduate Admission (30 分)
CPU affinity
The database is encapsulated by queryrunner simulation
CentOS installs MySQL database
LeetCode103——zigzagLevelOrder of binary tree
Her power series II UCLA Li Jingyi: the last thing women need to do is "doubt themselves"
Usage and precautions of accumulator used in spark
LeetCode160 & LeetCode141——double pointers to solve the linked list
Dense passage retrieval for open domain question answering notes
redission看门狗实现机制一看就懂
Statistical analysis of offline log collection
What is Baidu snapshot hijacking? Baidu snapshot hijacking principle and solution
PAT-2021年冬季考试(满分)
1053 Path of Equal Weight (30 分)
本地镜像发布到阿里云