在 Python 中使用不带空格的 json.dumps()
使用 separators 关键字参数来使用不带空格的 json.dumps()
方法,例如 json_str = json.dumps(employee, separators=(',', ':'))
。 可以将 separators 参数设置为包含逗号和冒号的元组以删除空格。
import json
employee = {
'name': 'Alice',
'age': 30,
'salary': 100,
}
# 👉️ default separators are (', ', ': ')
# ✅ json string without spaces
json_str = json.dumps(employee, separators=(',', ':'))
print(json_str) # 👉️ '{"name":"Alice","age":30,"salary":100}'
# -----------------------------------------------
# ✅ json string with spaces only between keys and values
json_str = json.dumps(employee, separators=(',', ': '))
print(json_str) # ✅ '{"name": "Alice","age": 30,"salary": 100}'
# -----------------------------------------------
# ✅ json string with spaces only between key-value pairs
json_str = json.dumps(employee, separators=(', ', ':'))
print(json_str) # ✅ '{"name":"Alice", "age":30, "salary":100}'
我们使用 separator
关键字参数在 json.dumps() 方法的输出中不添加额外的空格。
json.dumps
方法将 Python 对象转换为 JSON 格式的字符串。
separators
参数是一个包含 2 个值的元组 - 每个键值对之间的分隔符以及每个键和值之间的分隔符。
如果 indent 参数为 None,则参数的默认值为 (', ', ': ')
;如果 indent 设置为任何其他值,则参数的默认值为 (',', ': ')
。
import json
employee = {
'name': 'Alice',
'age': 30,
'salary': 100,
}
# 👇️ default behavior
print(json.dumps(employee)) # 👇️ {"name": "Alice", "age": 30, "salary": 100}
print(len(json.dumps(employee))) # 👉️ 43
# 👇️ with whitespace removed
json_str = json.dumps(employee, separators=(',', ':'))
print(json_str) # 👉️ '{"name":"Alice","age":30,"salary":100}'
print(len(json_str)) # 👉️ 38
如果在转换为 JSON 时仍然出现空格,请确保没有为 indent 参数传递值。
import json
employee = {
'name': 'Alice',
'age': 30,
'salary': 100,
}
# {
# "name":"Alice",
# "age":30,
# "salary":100
# }
json_str = json.dumps(employee, indent=4, separators=(',', ':'))
print(len(json_str)) # 👉️ 54
indent 关键字参数漂亮地打印具有指定缩进级别的 JSON 字符串。 该参数默认设置为无。
要从 JSON 字符串中删除所有空格,请将
indent
参数设置为 None 或在调用json.dumps()
时将其省略。
如果我们只需要在键和值之间添加空格,请将分隔符设置为冒号和空格。
import json
employee = {
'name': 'Alice',
'age': 30,
'salary': 100,
}
json_str = json.dumps(employee, separators=(',', ': '))
print(json_str) # 👉️ '{"name": "Alice","age": 30,"salary": 100}'
print(len(json_str)) # 👉️ 41
同样,如果我们只需要在键值对之间添加空格,请将分隔符设置为逗号和空格。
import json
employee = {
'name': 'Alice',
'age': 30,
'salary': 100,
}
json_str = json.dumps(employee, separators=(', ', ':'))
print(json_str) # 👉️ '{"name":"Alice", "age":30, "salary":100}'
print(len(json_str)) # 👉️ 40
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。