alex_bn_lee

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

[982] Print and save a JSON file with indentation in Python

Certainly! To print a JSON file with indentation in Python, you can use the json module. Here’s an example of how to do it:

import json
# Your JSON data (replace this with your actual data)
data = {
"name": "John",
"age": 30,
"city": "New York"
}
# Convert the data to a formatted JSON string with indentation
formatted_json = json.dumps(data, indent=4)
# Print the formatted JSON
print(formatted_json)

In this example:

  • Replace the data dictionary with your actual JSON data.
  • The indent=4 argument in json.dumps() specifies the number of spaces for indentation (you can adjust it as needed).

When you run this code, it will print the JSON data with proper indentation. Feel free to replace the sample data with your own JSON content! 🐍📄


Example:

import json
dic = '{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::4283"}}, "features": [{"type": "Feature", "properties": {"Address": "Test, Property Address, 2000", "LotDP": "no lot / dp not given"}, "geometry": {"type": "Polygon", "coordinates": [[[152.26222229, -27.5630016919999], [152.261580527, -27.566587679], [152.25976944, -27.566330239], [152.260747395, -27.5608659609999], [152.26122243, -27.5611017049999], [152.261782341, -27.5615065539999], [152.262388274, -27.5620741769999], [152.26222229, -27.5630016919999]]]}}]}'
print(json.dumps(json.loads(dic), indent=4))

Output:

{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::4283"
}
},
"features": [
{
"type": "Feature",
"properties": {
"Address": "Test, Property Address, 2000",
"LotDP": "no lot / dp not given"
},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
152.26222229,
-27.5630016919999
],
[
152.261580527,
-27.566587679
],
[
152.25976944,
-27.566330239
],
[
152.260747395,
-27.5608659609999
],
[
152.26122243,
-27.5611017049999
],
[
152.261782341,
-27.5615065539999
],
[
152.262388274,
-27.5620741769999
],
[
152.26222229,
-27.5630016919999
]
]
]
}
}
]
}
{"name": "John", "age": 30, "city": "New York"}
{'name': 'John', 'age': 30, 'city': 'New York'}
{
"name": "John",
"age": 30,
"city": "New York"
}

Certainly! To save a Python dictionary to a JSON file with indentation, you can use the json module. Here’s an example of how to do it:

import json
# Sample dictionary
my_dict = {
"name": "John",
"age": 30,
"city": "New York"
}
# Specify the file path where you want to save the JSON data
output_file = "my_dict.json"
# Write the dictionary to the JSON file with indentation (4 spaces)
with open(output_file, "w") as json_file:
json.dump(my_dict, json_file, indent=4)
print(f"Saved dictionary to {output_file} with indentation.")

In this example:

  • Replace my_dict with your actual dictionary.
  • Adjust the output_file variable to the desired file path.
  • The indent=4 argument ensures that the JSON data is formatted with 4 spaces for readability.

After running this code, you’ll have a JSON file (my_dict.json) containing the dictionary data with proper indentation. 📝🐍

posted on   McDelfino  阅读(8)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2014-03-05 【139】论文查重修改技巧大全
点击右上角即可分享
微信分享提示