多条dict写入json文件的时候,怎么加换行符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多条dict写入json文件的时候,怎么加换行符相关的知识,希望对你有一定的参考价值。

import json
data = []
with open('target.json') as file:
for line in file:
data.append(json.loads(line))

with open('testoutput.json', 'w') as file:
for line in data:
json.dump(line, file)

最后一行写入的数据打开是一行。。我想每次写入一个line的时候,加个换行符。怎么加?

参考技术A for line in file:
data.append('\n'+json.loads(line))
这样先换行再读取下一行试试。

Python之dict(或对象)与json之间转化

在Python语言中,json数据与dict字典以及对象之间的转化,是必不可少的操作。

在Python中自带json库。通过import json导入。

在json模块有2个方法,

  • loads():将json数据转化成dict数据
  • dumps():将dict数据转化成json数据
  • load():读取json文件数据,转成dict数据
  • dump():将dict数据转化成json数据后写入json文件

 技术图片

下面是具体的示例:

dict字典转json数据

import json
 
def dict_to_json():
    dict = 
    dict[name] = many
    dict[age] = 10
    dict[sex] = male
    print(dict)  # 输出:‘name‘: ‘many‘, ‘age‘: 10, ‘sex‘: ‘male‘
    j = json.dumps(dict)
    print(j)  # 输出:"name": "many", "age": 10, "sex": "male"
  
if __name__ == __main__:
    dict_to_json()

对象转json数据

import json

def obj_to_json():
    stu = Student(007, 007, 28, male, 13000000000, [email protected])
    print(type(stu))  # <class ‘json_test.student.Student‘>
    stu = stu.__dict__  # 将对象转成dict字典
    print(type(stu))  # <class ‘dict‘>
    print(stu)  # ‘id‘: ‘007‘, ‘name‘: ‘007‘, ‘age‘: 28, ‘sex‘: ‘male‘, ‘phone‘: ‘13000000000‘, ‘email‘: ‘[email protected]
    j = json.dumps(obj=stu)
    print(j)  # "id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"


if __name__ == __main__:
    obj_to_json()

json数据转成dict字典

 

import json

def json_to_dict():
    j = "id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"
    dict = json.loads(s=j)
    print(dict)  # ‘id‘: ‘007‘, ‘name‘: ‘007‘, ‘age‘: 28, ‘sex‘: ‘male‘, ‘phone‘: ‘13000000000‘, ‘email‘: ‘[email protected]

if __name__ == __main__:
    json_to_dict()

 

json数据转成对象

import json

def json_to_obj():
    j = "id": "007", "name": "007", "age": 28, "sex": "male", "phone": "13000000000", "email": "[email protected]"
    dict = json.loads(s=j)
    stu = Student()
    stu.__dict__ = dict
    print(id:  + stu.id +  name:  + stu.name +  age:  + str(stu.age) +  sex:  + str(
        stu.sex) +  phone:  + stu.phone +  email:  + stu.email)  # id: 007 name: 007 age: 28 sex: male phone: 13000000000 email: [email protected]

if __name__ == __main__:
    json_to_obj()

json的load()dump()方法的使用

  • dump()方法的使用
import json

def dict_to_json_write_file():
    dict = 
    dict[name] = many
    dict[age] = 10
    dict[sex] = male
    print(dict)  # ‘name‘: ‘many‘, ‘age‘: 10, ‘sex‘: ‘male‘
    with open(1.json, w) as f:
        json.dump(dict, f)  # 会在目录下生成一个1.json的文件,文件内容是dict数据转成的json数据

if __name__ == __main__:
    dict_to_json_write_file()
  • load()的使用
import json

def json_file_to_dict():
    with open(1.json, r) as f:
        dict = json.load(fp=f)
        print(dict)  # ‘name‘: ‘many‘, ‘age‘: 10, ‘sex‘: ‘male‘

if __name__ == __main__:
    json_file_to_dict()

 

技术图片

技术图片

mport json
d1 = 
    en:英语,
    cn:中文,
    fr:法语,
    jp:日语

print(d1,d1)
j1 = json.dumps(d1) # dict 变 json
print(j1,j1)
d2 = json.loads(j1) # json 变 dict
print(d2,d2)
 
with open(test.json,w+) as f:
    json.dump(d1, f)
 
with open(test.json,r+) as f:
    d3 = json.load(f)
    print(d3,d3)
import json
d1 = en:英语,cn:中文,fr:法语,jp:日语
dict1 = "server":["hostname": "172.20.20.18", "username": "Agoly", "password": "123456",]

if __name__ == __main__:

    print (json.dumps(d1))
    print(json.dumps(dict1))

 

以上是关于多条dict写入json文件的时候,怎么加换行符的主要内容,如果未能解决你的问题,请参考以下文章

字典(JSON)数据写入文件并换行,Python

字典(JSON)数据写入文件并换行,Python

python怎么做到在写入的TXT中换行

cocos修改json文件

使用控制台将多条 JSON 记录加载到 BigQuery

python中dict怎么接收来自文件的内容