Python写入Json文件详解
1. 什么是Json文件?
Json(JavaScript Object Notation)是一种轻量级的数据交换格式,它使用人们熟悉的文本格式来表示数据。Json文件通常以.json
为扩展名,可以在多种编程语言中进行读写操作。
Json文件由键-值对(key-value pairs)组成,使用大括号 {}
包裹。每个键-值对中,键和值之间使用冒号 :
分隔,每个键-值对之间使用逗号 ,
分隔。值可以是字符串(需要用双引号 ""
包裹)、数字、布尔值、数组(用方括号 []
包裹)、对象(键-值对的集合)等等。
示例Json文件内容:
{
"name": "Lucy",
"age": 25,
"is_student": true,
"favorite_fruits": ["apple", "banana", "orange"],
"address": {
"city": "New York",
"street": "123 Main St"
}
}
2. Python写入Json文件的方法
Python提供了多种写入Json文件的方法,我们可以使用内置的json
库或第三方库(如pandas
)来实现。下面将详细介绍两种常用方法。
方法一:使用内置的json
库
json
库提供了dump()
和dumps()
两个函数,可以将数据写入Json文件。
示例代码1:
import json
data = {
"name": "Lucy",
"age": 25,
"is_student": True,
"favorite_fruits": ["apple", "banana", "orange"],
"address": {
"city": "New York",
"street": "123 Main St"
}
}
# 打开Json文件,使用写入方式
with open('data.json', 'w') as file:
json.dump(data, file)
运行结果1:
生成了名为data.json
的Json文件,内容如下:
{
"name": "Lucy",
"age": 25,
"is_student": true,
"favorite_fruits": ["apple", "banana", "orange"],
"address": {
"city": "New York",
"street": "123 Main St"
}
}
示例代码2:
如果想要格式化输出Json文件,可以使用indent
参数设置缩进的空格数。
import json
data = {
"name": "Lucy",
"age": 25,
"is_student": True,
"favorite_fruits": ["apple", "banana", "orange"],
"address": {
"city": "New York",
"street": "123 Main St"
}
}
# 打开Json文件,使用写入方式
with open('data.json', 'w') as file:
json.dump(data, file, indent=4)
运行结果2:
生成了名为data.json
的Json文件,格式化输出:
{
"name": "Lucy",
"age": 25,
"is_student": true,
"favorite_fruits": [
"apple",
"banana",
"orange"
],
"address": {
"city": "New York",
"street": "123 Main St"
}
}
方法二:使用pandas
库
pandas
是一个强大的数据处理和分析库,可以方便地将数据写入Json文件。
示例代码3:
import pandas as pd
data = {
"name": ["Lucy", "Tom", "John"],
"age": [25, 30, 28],
"is_student": [True, False, False],
"favorite_fruits": [["apple", "banana"], ["orange"], []],
"address": [
{"city": "New York", "street": "123 Main St"},
{"city": "London", "street": "456 Park Ave"},
{}
]
}
df = pd.DataFrame(data)
df.to_json('data.json', orient='records')
运行结果3:
生成了名为data.json
的Json文件,内容如下:
[{"name":"Lucy","age":25,"is_student":true,"favorite_fruits":["apple","banana"],"address":{"city":"New York","street":"123 Main St"}},{"name":"Tom","age":30,"is_student":false,"favorite_fruits":["orange"],"address":{"city":"London","street":"456 Park Ave"}},{"name":"John","age":28,"is_student":false,"favorite_fruits":[],"address":{}}]
示例代码4:
如果想要格式化输出Json文件,可以设置indent
参数。
import pandas as pd
data = {
"name": ["Lucy", "Tom", "John"],
"age": [25, 30, 28],
"is_student": [True, False, False],
"favorite_fruits": [["apple", "banana"], ["orange"], []],
"address": [
{"city": "New York", "street": "123 Main St"},
{"city": "London", "street": "456 Park Ave"},
{}
]
}
df = pd.DataFrame(data)
df.to_json('data.json', orient='records', indent=4)
运行结果4:
生成了名为data.json
的Json文件,格式化输出:
[
{
"name": "Lucy",
"age": 25,
"is_student": true,
"favorite_fruits": [
"apple",
"banana"
],
"address": {
"city": "New York",
"street": "123 Main St"
}
},
{
"name": "Tom",
"age": 30,
"is_student": false,
"favorite_fruits": [
"orange"
],
"address": {
"city": "London",
"street": "456 Park Ave"
}
},
{
"name": "John",
"age": 28,
"is_student": false,
"favorite_fruits": [],
"address": {}
}
]
3. 总结
本文详细介绍了如何使用Python将数据写入Json文件,并给出了使用内置的json
库和第三方库pandas
的示例代码及运行结果。