详解python读写json文件
作者:liulanba 发布时间:2022-11-01 16:18:53
标签:python,读写,json,文件
python处理json文本文件主要是以下四个函数:
函数 | 作用 |
---|---|
json.dumps | 对数据进行编码,将python中的字典 转换为 字符串 |
json.loads | 对数据进行解码,将 字符串 转换为 python中的字典 |
json.dump | 将dict数据写入json文件中 |
json.load | 打开json文件,并把字符串转换为python的dict数据 |
json.dumps / json.loads
数据转换对照:
json | python |
---|---|
object | dict |
array | list |
string | str |
number (int) | int |
number (real) | float |
true | True |
false | False |
null | None |
代码示例:
import json
tesdic = {
'name': 'Tom',
'age': 18,
'score':
{
'math': 98,
'chinese': 99
}
}
print(type(tesdic))
json_str = json.dumps(tesdic)
print(json_str)
print(type(json_str))
newdic = json.loads(json_str)
print(newdic)
print(type(newdic))
输出为:
<class 'dict'>
{"name": "Tom", "age": 18, "score": {"math": 98, "chinese": 99}}
<class 'str'>
{'name': 'Tom', 'age': 18, 'score': {'math': 98, 'chinese': 99}}
<class 'dict'>
json.dump / json.load
写入json的内容只能是dict类型,字符串类型的将会导致写入格式问题:
with open("res.json", 'w', encoding='utf-8') as fw:
json.dump(json_str, fw, indent=4, ensure_ascii=False)
则json文件内容为:
"{\"name\": \"Tom\", \"age\": 18, \"score\": {\"math\": 98, \"chinese\": 99}}"
我们换一种数据类型写入:
with open("res.json", 'w', encoding='utf-8') as fw:
json.dump(tesdic, fw, indent=4, ensure_ascii=False)
则生成的josn就是正确的格式:
{
"name": "Tom",
"age": 18,
"score": {
"math": 98,
"chinese": 99
}
}
同理,从json中读取到的数据也是dict类型:
with open("res.json", 'r', encoding='utf-8') as fw:
injson = json.load(fw)
print(injson)
print(type(injson))
{'name': 'Tom', 'age': 18, 'score': {'math': 98, 'chinese': 99}}
<class 'dict'>
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!
来源:https://blog.csdn.net/liulanba/article/details/121963892
0
投稿
猜你喜欢
- tkinter的功能是如此强大,竟然还能做翻译软件。当然是在线的,我发现有一个quicktranslate模块,可以提供在线翻译功能,相当于
- 本文实例讲述了Symfony2实现从数据库获取数据的方法。分享给大家供大家参考,具体如下:假设有一张表:test, 字段:name,colo
- 一、前言在Python提供了re模块,用于实现正则表达式的操作。在实现时,可以使用re模块提供的方法(如,search()、match()、
- 今天主要是来说一下怎么可视化来监控你的爬虫的状态。相信大家在跑爬虫的过程中,也会好奇自己养的爬虫一分钟可以爬多少页面,多大的数据量,当然查询
- 前言把以前一直只限于知道,却不清晰理解的这几个概念完完整整地梳理了一番。内容参考自wiki页面,然后加上自己一些理解。词法作用域和动态作用域
- 本文介绍如果使用python汇总常用的图表,与Excel的点选操作相比,用python绘制图表显得比较比较繁琐,尤其提现在对原始数据的处理上
- KNN算法算是最简单的机器学习算法之一了,这个算法最大的特点是没有训练过程,是一种懒惰学习,这种结构也可以在tensorflow实现。KNN
- 由于连续的字符(字母、符号、数字)在默认情况下是不换行的,可能会破坏整个界面布局。那如何解决这个问题呢?在 IE 和 Safari 1.3+
- 简单的XML操作:XML文件创建把下面的代码复制到按钮事件中编译执行后可在相应物理路径中产生Pos.xml文件XmlTextWriter x
- 本文实例讲述了Python2.7中SQLite3基本操作方法。分享给大家供大家参考,具体如下:1、基本操作# -*- coding: utf
- 本文列出了初学网页编程中常用到的一些代码和一些技巧,简单实用,您一定用得到。1、oncontextmenu="window.eve
- 今天写了个小功能,看起来挺简单,写的过程中发现了些坑。1.div没有disabled的属性,所以得写成button2.disabled在da
- 在教材实例编写雷达图时出现ValueError,具体如下:ValueError: The number of FixedLocator lo
- 目的:方便调试,查看中间结果,因为觉得设断点调试相对麻烦。【运行环境:macOS 10.13.3,PyCharm 2017.2.4】老手:选
- python中字典可以一键多值,也就是意味着一个键可以对应多个值。例:#encoding=utf-8print '中国'#字
- PyAutoGUI是一个纯Python的GUI自动化工具,其目的是可以用程序自动控制鼠标和键盘操作,多平台支持(Windows,OS X,L
- 【导语】:对自己写的冗长代码,想重构但又无思路?小编整理了系列介绍python代码重构优化的方法,助你一臂之力。编写干净的 Pythonic
- 第一种是进行多项式拟合,数学上可以证明,任意函数都可以表示为多项式形式。具体示例如下。###拟合年龄import numpy as npim
- VIM python下的一些关于缩进的设置:第一步: 打开终端,在终端上输入vim ~/.vimrc,回车。 第二步: 添加下面的文段:se
- 报错现象File "<string>", line 1SyntaxError: unexpected EOF