给我一个python输出成json文件的示例
- User
给我一个python输出成json文件的示例
Assistant
Python 输出JSON文件的示例
以下是一个完整的Python示例,展示如何将数据输出为JSON文件:
import json # 示例数据 data = { "name": "John Doe", "age": 30, "is_student": False, "courses": ["Math", "Science", "History"], "address": { "street": "123 Main St", "city": "New York", "zip": "10001" }, "scores": [85, 92, 78, 95] } # 将数据写入JSON文件 with open('output.json', 'w', encoding='utf-8') as json_file: # 使用indent参数使输出更易读 json.dump(data, json_file, indent=4, ensure_ascii=False) print("JSON文件已成功创建: output.json")运行结果
运行上述代码后,会在当前目录下生成一个名为
output.json的文件,内容如下:{ "name": "John Doe", "age": 30, "is_student": false, "courses": [ "Math", "Science", "History" ], "address": { "street": "123 Main St", "city": "New York", "zip": "10001" }, "scores": [ 85, 92, 78, 95 ] }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.