博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
14.json文件读取
阅读量:7123 次
发布时间:2019-06-28

本文共 1090 字,大约阅读时间需要 3 分钟。

json文件读取
1.#读取jsonimport jsonstr='''[    {    "name":"Tom",    "gender":"male",    "birth":"1997-12-13"    },    {    "name": "Jerry",    "gender": "male",    "birth": "1998-10-18"    }]'''#注意问题  json字符串的表示需要用双引号,否则loads()方法会解析失败print(type(str))data=json.loads(str)print(data)print(type(data))print(data[0]['birth'])

执行结果如图:

 

 

2.#读取jsonimport jsondata=[    {    'name':'Tom',    'gender':'male',    'birth':'1997-12-13'    },    {    'name': 'Jerry',    'gender': 'male',    'birth': '1998-10-18'    }]# 将json对象转为字符串,然后调用文件的write()方法写入文本with open('data.json','w')as file:    file.write(json.dumps(data))

运行结果如图:

 

 

3.#读取jsonimport jsondata=[    {    'name':'德玛西亚',    'gender':'male',    'birth':'1997-12-13'    },    {    'name': 'Jerry',    'gender': 'male',    'birth': '1998-10-18'    }]#保存json格式,添加一个参数indent,代表缩进字符个数。#为了输出中文 指定参数 ensure_ascii=Falsewith open('data.json3','w')as file:    file.write(json.dumps(data,indent=2))    # file.write(json.dumps(data,indent=2,ensure_ascii=False))

 

执行结果如图:

 

posted on
2018-09-26 18:21 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/lvjing/p/9708779.html

你可能感兴趣的文章
副高职称论文发表
查看>>
好程序员web前端教程分享js模板模式
查看>>
5款windows神器吐血推荐!个个都是精品
查看>>
Oracle教程之深入Shared Pool
查看>>
CentOS Cacti网络监控服务安装与配置方法
查看>>
service worker
查看>>
我的友情链接
查看>>
结对(第二周)
查看>>
年底了冲刺中。。。
查看>>
Linux正确的关机方法
查看>>
EJB、DCOM、CORBA 三种技术分析 (转)
查看>>
grep 去除注释行和空行
查看>>
什么是手机和Mobile Web的UA识别
查看>>
我的友情链接
查看>>
Linux用户和组的操作(三) 用户组文件/etc/group
查看>>
如何在windows上创建能够编译的hypertable工程
查看>>
Linux下文件的三个时间(Atime,Mtime,Ctime)
查看>>
我的友情链接
查看>>
python之字符串操作方法
查看>>
KVM---GUI远程维护管理
查看>>