对python中dict和json的区别详解

yipeiwu_com6年前Python基础

1、json 和 字典 区别

>>>import json

>>>json.dumps({1:2})

>>>'{"1":2}'

--------------------

>>>{1:2}

>>>{1:@}

其中字典的格式是字典,json的格式是字符串,在传输的时候用的是字符串,所以如果要传输字典内容,就需要先进行字典转json。

json中必须使用双引号,dict则可以用单引号也可以用双引号

2、json.dump()/json.load() 和 json.dumps()/json.loads()区别

json.dumps()/json.loads()用来编码和解码json字符串数据

json.dump()/json.load()用来处理文件

eg:

import json
json_content = {'a':'1111','b':'2222','c':'3333','d':'4444'}
with open('json_file.json','w') as f:
 json.dump(json_content, f)
with open('json_file.json', 'r') as f:
 content = json.load(f)
 print(content)

以上这篇对python中dict和json的区别详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用knn实现特征向量分类

这是一个使用knn把特征向量进行分类的demo。 Knn算法的思想简单说就是:看输入的sample点周围的k个点都属于哪个类,哪个类的点最多,就把sample归为哪个类。也就是说,训练集...

详解python uiautomator2 watcher的使用方法

该方是基于uiautomator2如下版本进行验证的: PS C:\windows\system32> pip show uiautomator2 Name: uiautoma...

Python递归遍历列表及输出的实现方法

本文实例讲述了Python递归遍历列表及输出的实现方法。分享给大家供大家参考。具体实现方法如下: def dp(s): if isinstance(s,(int,str)):...

pytorch forward两个参数实例

以channel Attention Block为例子 class CAB(nn.Module): def __init__(self, in_channels, out_c...

Python将列表数据写入文件(txt, csv,excel)

写入txt文件 def text_save(filename, data):#filename为写入CSV文件的路径,data为要写入数据列表. file = open(file...