python中json操作
1、写操作.json文件dumps()、dump()函数
d = {
'zll': {
'addr': '北京',
'age': 28
},
'ljj': {
'addr': '北京',
'age': 38
}
}
fw = open('user_info.json', 'w', encoding='utf-8')
# ensure_ascii:默认值True,如果dict内含有non-ASCII的字符,则会类似\uXXXX的显示数据,设置成False后,就能正常显示
# dic_json = json.dumps(d,ensure_ascii=False,indent=4) #字典转成json格式,字典转成字符串
dic_json = json.dumps(d, ensure_ascii=True, indent=4) # 字典转成json格式,字典转成字符串
fw.write(dic_json)
6 fw.close()
结果
1 {
2 "zll": {
3 "addr": "\u5317\u4eac",
4 "age": 28
5 },
6 "ljj": {
7 "addr": "\u5317\u4eac",
8 "age": 38
9 }
10 }
fw = open('user_info.json', 'w', encoding='utf-8')
# ensure_ascii:默认值True,如果dict内含有non-ASCII的字符,则会类似\uXXXX的显示数据,设置成False后,就能正常显示
dic_json = json.dumps(d,ensure_ascii=False,indent=4) #字典转成json,字典转成字符串
fw.write(dic_json)
6 fw.close()
结果
{ "zll": { "addr": "北京", "age": 28 }, "ljj": { "addr": "北京", "age": 38 } }
dump() 操作json文件 写的操作
fw = open('user_info.json', 'w', encoding='utf-8')
json.dump(d,fw,ensure_ascii=False,indent=4) #操作文件
fw.close()
结果
{
"zll": {
"addr": "北京",
"age": 28
},
"ljj": {
"addr": "北京",
"age": 38
}
}
2、读操作load()、loads()
# json串是一个字符串
f = open('product.json',encoding='utf-8')
res = f.read()
product_dic = json.loads(res) #把json串,变成python的数据类型,只能转换json串内容
print(product_dic)
print(product_dic['iphone'])
# t = json.load(f)
# print(t) #传一个文件对象,它会帮你直接读json文件,并转换成python数据
# print(t['iphone'])
f.close() 结果
{'iphone': {'color': 'red', 'num': 1, 'price': 98.5}, 'wather': {'num': 100, 'price': 1, 'color': 'white'}}
{'color': 'red', 'num': 1, 'price': 98.5}
# 文件读写
def op_file(file, dict_temp=None):
if dict_temp:
with open(file, 'w', encoding='utf-8') as fw:
json.dump(dict_temp, fw, ensure_ascii=False, indent=4)
return 1
else:
with open(file, 'r', encoding='utf-8') as fr:
dict_temp = json.load(fr)
return dict_temp
python中json操作的更多相关文章
- python中json操作了解
什么是接口? 交换数据 http://openweathermap.org/current json简介 JSON 是存储和交换文本信息的语法.类似 XML JSON 语法是 JavaScript 语 ...
- Python中json的简单读写操作
Python中json的简单读写操作 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的 ...
- Neo4j:图数据库GraphDB(四)Python中的操作
本文总结下Python中如何操作Neo4j数据库,用到py2neo包,Pip install 一下. 1 连接neo4j数据库:跟其它数据库一样,操作前必须输入用户名和密码及地址连接一下. from ...
- python中json格式数据输出实现方式
python中json格式数据输出实现方式 主要使用json模块,直接导入import json即可. 小例子如下: #coding=UTF-8 import json info={} info[&q ...
- python中文件操作的六种模式及对文件某一行进行修改的方法
一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...
- python中文件操作的其他方法
前面介绍过Python中文件操作的一般方法,包括打开,写入,关闭.本文中介绍下python中关于文件操作的其他比较常用的一些方法. 首先创建一个文件poems: p=open('poems','r', ...
- python MySQLdb用法,python中cursor操作数据库(转)
数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_N ...
- (数据科学学习手札126)Python中JSON结构数据的高效增删改操作
本文示例代码及文件已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在上一期文章中我们一起学习了在Python ...
- python中json的操作示例
先上一段示例 # -*- coding: cp936 -*- import json #构造一个示例数据,并打印成易读样式 j = {} j["userName"]="a ...
随机推荐
- TestNG+ReportNG+Maven优化测试报告
转载:https://www.cnblogs.com/hardy-test/p/5354733.html 首先在eclipse里面创建一个maven项目,具体要配置maven环境,请自行百度搭配环境. ...
- 倍福TwinCAT(贝福Beckhoff)应用教程12.2 TwinCAT控制松下伺服 NC初步
在前面我们已经学会了使用贝福自带的调试软件完成试运行,接下来是使用TWINCAT PLC实现这个功能,右击PLC添加一个PLC项目 在VISUs上右击添加一个HMI人机界面 目前PLC程序和人 ...
- redux-actions
其作用都是用来简化action.reducer. 1.安装 npm install --save redux-actions // 或 yarn add redux-actions 2.使用 crea ...
- python 爬取王者荣耀高清壁纸
代码地址如下:http://www.demodashi.com/demo/13104.html 一.前言 打过王者的童鞋一般都会喜欢里边设计出来的英雄吧,特别想把王者荣耀的英雄的高清图片当成电脑桌面 ...
- GoldenGate Lag For Huge Insert
前些天客户的ogg延迟到达8小时左右.于是我当时用logdump追踪了一下: 看进程状态: send extsa staus EXTRACT ZBDBA (PID 2269368) Current s ...
- web info
http://blog.csdn.net/qq_24473141/article/details/51363662 http://blog.sina.com.cn/s/blog_8e392fc2010 ...
- Angularjs学习笔记7_directive1
1.基础知识 directive()接受两个参数 · name:字符串,指令的名字 · factory_function:函数,指令的行为 应用启动时,以name作为该应用的标识注册factory_f ...
- 严重: Error in dependencyCheck java.io.IOException: invalid header field(tomcat启动成功可是訪问web项目404错误)
tomcat启动的时候出现 严重: Error in dependencyCheck java.io.IOException: invalid header field 而且tomcat也不自己主动r ...
- 167. Two Sum II - Input array is sorted【easy】
167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...
- vm中安装ubuntu16
安装完成后,开机出现 smbus host controller not enabled 上网照着网友的建议: 在grub界面选择高级选项 root shell 进入到shell之后 mount -o ...