python将字典写入文件以及从文件中读取字典数据
时间: 2024-02-03 19:09:58 浏览: 130
Python txt文件加入字典并查询的方法
以下是Python将字典写入文件以及从文件中读取字典数据的示例:
1. 将字典写入文件:
```python
import json
data = {"name": "John", "age": 30, "city": "New York"}
with open("data.json", "w") as file:
json.dump(data, file)
```
2. 从文件中读取字典数据:
```python
import json
with open("data.json", "r") as file:
data = json.load(file)
print(data)
```
阅读全文