Python json 读取 json 文件并转为 dict
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:33
Python json 读取 json 文件并转为 dict
在 D 盘 新建 test.json:
{
"test": "测试\n换行",<br/>
"dict": {<br/>
"list": [0, "str\""],<br/>
"num": 0<br/>
}<br/>
}
json 格式编写:
json 格式大致以 python 的 dict {} 格式来编写即可,只是要注意字符串不能用单引号‘ ’,一定要用双引号“ ”
字符串支持转义
import sys
import os
import json p = r‘d:\test.json’
if os.path.exists(p):
if sys.version_info.major > 2:<br/> f = open(p, 'r', encoding = 'utf-8')<br/> else:<br/> f = open(p, 'r')<br/> dict_data = json.load(f)<br/> #or<br/> dict_data = json.loads(f.read())<br/> print(dict_data)</pre>注意:
json 的 load() 和 loads() 的区别
Python 2 和 3 的 open() 的区别
- 上一篇: Python json序列化
- 下一篇: python gui 实现sql查询
相关文章
-
Python json序列化
Python json序列化
- 互联网
- 2026年04月04日
-
python mac 获取屏幕图片
python mac 获取屏幕图片
- 互联网
- 2026年04月04日
-
python matplotlib三维图平滑
python matplotlib三维图平滑
- 互联网
- 2026年04月04日
-
python gui 实现sql查询
python gui 实现sql查询
- 互联网
- 2026年04月04日
-
Python gui 环境
Python gui 环境
- 互联网
- 2026年04月04日
-
Python GDAL库在Anaconda环境中的配置
Python GDAL库在Anaconda环境中的配置
- 互联网
- 2026年04月04日






