python读写json文件详解

在Python中,可以使用json模块来读写JSON格式的文件。下面是一个详细的示例,演示了如何读写JSON文件:

python 复制代码
import json

# 写入JSON文件
data = {
    "name": "John",
    "age": 30,
    "city": "New York"
}

with open('data.json', 'w') as f:
    json.dump(data, f)

# 读取JSON文件
with open('data.json', 'r') as f:
    loaded_data = json.load(f)
    print(loaded_data)

在这个示例中,首先定义了一个字典data,然后使用json.dump将数据写入到名为data.json的文件中。接着使用json.load读取data.json文件,并将读取的数据加载到loaded_data变量中,最后打印出加载的数据。

相关推荐
helloweilei7 小时前
python 抽象基类
python
用户8356290780517 小时前
Python 实现 PPT 转 HTML
后端·python
zone773913 小时前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone773913 小时前
005:RAG 入门-LangChain读取表格数据
后端·python·agent
树獭非懒1 天前
AI大模型小白手册|Embedding 与向量数据库
后端·python·llm
唐叔在学习1 天前
就算没有服务器,我照样能够同步数据
后端·python·程序员
曲幽1 天前
FastAPI流式输出实战与避坑指南:让AI像人一样“边想边说”
python·ai·fastapi·web·stream·chat·async·generator·ollama
Flittly1 天前
【从零手写 AI Agent:learn-claude-code 项目实战笔记】(1)The Agent Loop (智能体循环)
python·agent
vivo互联网技术1 天前
ICLR2026 | 视频虚化新突破!Any-to-Bokeh 一键生成电影感连贯效果
人工智能·python·深度学习