[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 injson.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. 📝🐍
分类:
Python Study
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2014-03-05 【139】论文查重修改技巧大全