json和pickle模块

内容目录

一、JSON 1

1.1 json.dumps() 1

1.2 json.loads() 2

1.3 json.dump() 2

1.4 json.load() 3

1.5编码解码规则 5

二、pickle 5

2.1 pickle.dumps() 5

2.2 pickle.loads() 5

2.3 pickle.dump() 6

2.3 pickle.load() 6

一、JSON

JSON是一种轻量级的数据交换格式,易于人阅读和编写,也易于机器解析和生成。

1.1 json.dumps()

编码:把一个Python对象编码转换成json字符串。

json.dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Serialize obj to a JSON formatted str using this conversion table. The arguments have the same meaning as in dump().
压缩
import json
li = [1, 2, 3, {‘4‘: 5, ‘6‘: 7}]
ret = json.dumps(li, separators=(‘,‘, ‘:‘), sort_keys=True)
print(ret)  # [1,2,3,{"4":5,"6":7}]
缩进
import json
li = [1, 2, 3, {‘4‘: 5, ‘6‘: 7}]
ret = json.dumps(li,indent=4)
print(ret)
[
    1,
    2,
    3,
    {
        "6": 7,
        "4": 5
}
]
排序
import json
li = [1, 2, 3, {‘4‘: 5, ‘6‘: 7}]
ret = json.dumps(li, indent=4, sort_keys=True)
print(ret)
[
    1,
    2,
    3,
    {
        "4": 5,
        "6": 7
}
]

1.2 json.loads()

解码:把json格式字符串转换成Python对象

json.loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize s (a str instance containing a JSON document) to a Python object using this conversion table.
The other arguments have the same meaning as in load(), except encoding which is ignored and deprecated.
If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.
st1 = ‘{"__complex__": true, "real": 1, "imag": 2}‘
ret = json.loads(st1)
print(ret)  # {‘real‘: 1, ‘__complex__‘: True, ‘imag‘: 2}

1.3 json.dump()

处理文件编码

json.dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)
Serialize obj as a JSON formatted stream to fp (a .write()-supporting file-like object) using this conversion table.
If skipkeys is True (default: False), then dict keys that are not of a basic type (str, int, float, bool, None) will be skipped instead of raising a TypeError.
The json module always produces str objects, not bytes objects. Therefore, fp.write() must support str input.
If ensure_ascii is True (the default), the output is guaranteed to have all incoming non-ASCII characters escaped. If ensure_ascii is False, these characters will be output as-is.
If check_circular is False (default: True), then the circular reference check for container types will be skipped and a circular reference will result in an OverflowError (or worse).
If allow_nan is False (default: True), then it will be a ValueError to serialize out of range float values (nan, inf, -inf) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (NaN, Infinity, -Infinity).
If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0, negative, or "" will only insert newlines. None (the default) selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as "\t"), that string is used to indent each level.
Changed in version 3.2: Allow strings for indent in addition to integers.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘,‘, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,‘, ‘:‘) to eliminate whitespace.
Changed in version 3.4: Use (‘,‘, ‘: ‘) as default if indent is not None.
default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.
If sort_keys is True (default: False), then the output of dictionaries will be sorted by key.
To use a custom JSONEncoder subclass (e.g. one that overrides the default() method to serialize additional types), specify it with the cls kwarg; otherwise JSONEncoder is used.
li = [1, 2, 3]
json.dump(li, open(‘db‘, ‘w‘))
with open(‘db1‘, ‘w‘) as f:
json.dump(li, f)

1.4 json.load()

处理文件解码

json.load(fp, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw)
Deserialize fp (a .read()-supporting file-like object containing a JSON document) to a Python object using this conversion table.
object_hook is an optional function that will be called with the result of any object literal decoded (a dict). The return value of object_hook will be used instead of the dict. This feature can be used to implement custom decoders (e.g. JSON-RPC class hinting).
object_pairs_hook is an optional function that will be called with the result of any object literal decoded with an ordered list of pairs. The return value of object_pairs_hook will be used instead of the dict. This feature can be used to implement custom decoders that rely on the order that the key and value pairs are decoded (for example, collections.OrderedDict() will remember the order of insertion). If object_hook is also defined, the object_pairs_hook takes priority.
Changed in version 3.1: Added support for object_pairs_hook.
parse_float, if specified, will be called with the string of every JSON float to be decoded. By default, this is equivalent to float(num_str). This can be used to use another datatype or parser for JSON floats (e.g. decimal.Decimal).
parse_int, if specified, will be called with the string of every JSON int to be decoded. By default, this is equivalent to int(num_str). This can be used to use another datatype or parser for JSON integers (e.g. float).
parse_constant, if specified, will be called with one of the following strings: ‘-Infinity‘, ‘Infinity‘, ‘NaN‘. This can be used to raise an exception if invalid JSON numbers are encountered.
Changed in version 3.1: parse_constant doesn’t get called on ‘null’, ‘true’, ‘false’ anymore.
To use a custom JSONDecoder subclass, specify it with the cls kwarg; otherwise JSONDecoder is used. Additional keyword arguments will be passed to the constructor of the class.
If the data being deserialized is not a valid JSON document, a JSONDecodeError will be raised.
li = [1, 2, 3]
ret = json.load(open(‘db‘, ‘r‘))
print(ret, type(li))  # [1, 2, 3] <class ‘list‘>
with open(‘db1‘, ‘r‘) as f:
    ret = json.load(f)
print(ret, type(ret))  # [1, 2, 3] <class ‘list‘>

1.5 编码解码规则

二、pickle

The
pickle
module implements binary protocols for serializing and de-serializing
a Python object structure. “Pickling” is the process
whereby a Python object hierarchy is converted into a byte stream,
and “unpickling” is the inverse operation, whereby a
byte stream (from a binary file or bytes-like object) is converted
back into an object hierarchy. Pickling (and unpickling) is
alternatively known as “serialization”, “marshalling,” [1]
or “flattening”; however, to avoid confusion, the terms used here
are “pickling” and “unpickling”.

pickle模块实现了序列化和反序列化python对象结构。通过python模块的序列化操作能将程序中运行的对象信息保存到文件中,永久存储;通过pickle模块的反序列化操作,能从文件中创建上次保存的对象。

2.1 pickle.dumps()

pickle.dumps(obj,
protocol=None, *, fix_imports=True)
Return the pickled
representation of the object as a bytes
object, instead of writing it to a file.
Arguments protocol
and fix_imports have the same meaning as in dump().
import pickle
li = [1, 2, 3]
r = pickle.dumps(li)
print(r)  # b‘\x80\x03]q\x00(K\x01K\x02K\x03e.‘

2.2 pickle.loads()

pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
Read a pickled object hierarchy from a bytes object and return the reconstituted object hierarchy specified therein.
The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.
Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is true, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.
r = pickle.loads(r)
print(r)  # [1, 2, 3]

2.3 pickle.dump()

pickle.dump(obj, file, protocol=None, *, fix_imports=True)
Write a pickled representation of obj to the open file object file. This is equivalent to Pickler(file, protocol).dump(obj).
The optional protocol argument, an integer, tells the pickler to use the given protocol; supported protocols are 0 to HIGHEST_PROTOCOL. If not specified, the default is DEFAULT_PROTOCOL. If a negative number is specified, HIGHEST_PROTOCOL is selected.
The file argument must have a write() method that accepts a single bytes argument. It can thus be an on-disk file opened for binary writing, an io.BytesIO instance, or any other custom object that meets this interface.
If fix_imports is true and protocol is less than 3, pickle will try to map the new Python 3 names to the old module names used in Python 2, so that the pickle data stream is readable with Python 2.

2.3 pickle.load()

pickle.load(file, *, fix_imports=True, encoding="ASCII", errors="strict")
Read a pickled object representation from the open file object file and return the reconstituted object hierarchy specified therein. This is equivalent to Unpickler(file).load().
The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.
The argument file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return bytes. Thus file can be an on-disk file opened for binary reading, an io.BytesIO object, or any other custom object that meets this interface.
Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is true, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.
import pickle
li = [1, 2, 3]
pickle.dump(li, open(‘db‘, ‘wb‘))
ret = pickle.load(open(‘db‘, ‘rb‘))
print(ret)
时间: 2024-11-03 21:13:23

json和pickle模块的相关文章

python模块(json和pickle模块)

json和pickle模块,两个都是用于序列化的模块 • json模块,用于字符串与python数据类型之间的转换 • pickle模块,用于python特有类型与python数据类型之间的转换 两个模块,都提供了dumps,dump,loads,load 4个功能 1 import json 2 s = '{"key1":"value1","key2":"value2"}' # ==> 用json模块将字符串转化成其他

json与pickle模块

Python-19 1. json与pickle模块 什么是序列化和反序列化 序列化:是将内存中的数据结构,转换成一种中间格式,将转化后的中间格式存储到硬盘,或者基于网络传输 反序列化:是将硬盘中或者网路中传过来的中间格式,转换成内存中的数据结构 2. 序列化和反序列化有什么用 将状态通过中间格式存储到硬盘,可以保存程序的运行状态 数据可以跨平台交互,不同的编程语言,通过序列化成中间格式就可以互相交互 存到硬盘的文本文件都是字符串,再次使用时,需要将硬盘中的数据转化成以前的数据类型状态 3. j

python-时间模块,random、os、sys、shutil、json和pickle模块

一.time与datetime模块 time模块: 时间戳:表示的是从1970年1月1日00:00:00开始按秒计算的偏移量,返回类型为float类型 格式化时间字符串(Format String) 结构化的时间(struct_time):struct_time元组共有9个元素(年月日时分秒,一年中的第几周,一年中的第几天,夏令时) # print(time.time())#1533962144.060534 # print(time.localtime())#time.struct_time(

shutil模块、json和pickle模块

shutil模块: 高级的文件.文件夹.压缩包处理模块 json和pickle模块 之前学过eval内置方法可以将一个字符串转化成Python对象,但eval方法是有局限性的,对于普通的数据类型,json.loads.eval都可以使用,但遇到特殊类型的时候,eval就不能使用了, 所以eval的重点通常还是用来执行一个字符串表达式,并返回表达式的值. 序列化:我们把对象从内存中变成可存蓄或可传送的过程称为序列化,在Python中叫picking,在其他语言中叫serialization .ma

Python数据对象的编码和解码,json和pickle模块,base64模块的简单使用

1.面向对象 对象:生活中的客观事物 类:对事物的抽象,在代码中实现class类型 类属性:这类事物具有的特点或者属性 类方法:这类事物具有的行为,可以实现的方法 实例:使用之前对类的实例化之后的结果 实例属性:对象具有的一些描述对象或者形容对象的属性,对象具体具有的特性 实例方法:对象具有的方法,行为动作 1.查看对象所拥有的方法 dir(对象) 例如 print(dir(列表))1.类中的实例(类)属性和方法命名风格 属性:名词 方法:动词 2.Python中万物皆对象 _对象名,对象私有化

讲解 json 和 pickle 模块

首先是引入json 和 pickle 的原因是 普通的方法支持的数据类型太少 局限性大  比如下面的例子 dit = {'name':'deng1mei','age':'26','sex':'girl'} #创建一个字典dit = str(dit) #将字典字符串化 以方便写入文件# f= open ('test','w') #创建文件# f.write(dit) #write() argument must be str, not dict #写入文件f=open('test','r') #句

Python基础-json和pickle模块

一 .序列化是指把内存里的数据类型转变成字符串,以使其能存储到硬盘或通过网络传输到远程,因为硬盘或网络传输时只能接受bytes 把字符串转化成内存数据类型 叫做反序列化 json 和 pickle 二. 只是把数据类型转化成字符串存到内存里的意义 json.dumps  json.loads 1.把你的内存数据通过网络共享给远程其他人 2.定义了不同语言的交互规则 a.纯文本,坏处,不能共享复杂的数据类型 b.xml,坏处,占用空间大 c.json,简单,可读性好 三.json与pickle的区

Python基础六--JSON, pickle模块

一.JSON 内存中的数据<--->格式json<--->字符类型<--->保存.基于网络传输 1. 将数据转化为str形式:data_str = json.dumps(data): 2. 将str形式数据转化为字典等数据:data = json.loads(data_str): 3. 注意json格式 :data = '{"name":"gangzi"}' : 二.pickle (只应用于Python,不同版本的Python彼此

序列化的两个模块(json和pickle)

到底什么是序列化(picking)呢? 我们把变量从内存中变成可存储或传输的过程称之为序列化 序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上. 反过来,把变量内容从序列化的对象重新读到内存里称之为反序列化,即unpickling json和pickle json和pickle模块,两个都是用于序列化的模块 json #json 是通用的,可以在各种语言里进行交互,只是一个简单的序列化方法#json把python对象转化成字符串,仅限于简单的数据类型,例如列表,字典,元组