python-fullstack-s13-day24-python模块部分

Posted bug-ming

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-fullstack-s13-day24-python模块部分相关的知识,希望对你有一定的参考价值。

Json模块提供了四个功能:dumps、dump、loads、load
1
import json 2 # dumps loads一般用于网络传输数据使用,可在多语言环境中使用,但是只支持str,dict,list,bool 3 dic = {"alex": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"} 4 ret = json.dumps(dic) 5 ret2 = json.dumps(dic, ensure_ascii=False) # dumps()括号内可加参数,indent缩进量等参数 6 print(ret) # {"alex": ["woman", "man", "lady_boy"], "age": 13, "hobby": "u8001u5973u4eba"} 7 print(ret2) # {"alex": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"} 8 dic_ret = json.loads(ret) 9 print(dic_ret) # {‘alex‘: [‘woman‘, ‘man‘, ‘lady_boy‘], ‘age‘: 13, ‘hobby‘: ‘老女人‘}
 1 import json
 2 # 利用json的dumps loads向文件中写多条数据读取多行数据
 3 dic = {"alex": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
 4 dic1 = {"alex1": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
 5 dic2 = {"alex2": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
 6 with open("dumps", mode="w", encoding="utf-8") as f:
 7     f.write(json.dumps(dic) + "
")
 8     f.write(json.dumps(dic1) + "
")
 9     f.write(json.dumps(dic2) + "
")
10 
11 with open("dumps", encoding="utf-8") as f1:
12     for line in f1:
13         print(json.loads(line))
 1 import json
 2 
 3 dic = {"alex": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
 4 dic1 = {"alex1": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
 5 dic2 = {"alex2": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
 6 with open("file", mode="w", encoding="utf-8") as f:
 7     json.dump(dic, f)
 8     # json.dump(dic1, f)
 9     # json.dump(dic2, f)
10 # 能够成功向文件中写入多条数据,但都是写到一行上,而外读取的时候会发生错误。只有写入时是一条数据的时候不会发生错误。
11 with open("file", encoding="utf-8") as f2:
12     print(json.load(f2))



Serialize obj to a JSON formatted str.(字符串表示的json对象)
Skipkeys:默认值是False,如果dict的keys内的数据不是python的基本类型(str,unicode,int,long,float,bool,None),设置为False时,就会报TypeError的错误。此时设置成True,则会跳过这类key
ensure_ascii:,当它为True的时候,所有非ASCII码字符显示为uXXXX序列,只需在dump时将ensure_ascii设置为False即可,此时存入json的中文即可正常显示。)
If check_circular is false, 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, 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).
indent:应该是一个非负的整型,如果是0就是顶格分行显示,如果为空就是一行最紧凑显示,否则会换行且按照indent的数值显示前面的空白分行显示,这样打印出来的json数据也叫pretty-printed json
separators:分隔符,实际上是(item_separator, dict_separator)的一个元组,默认的就是(‘,’,’:’);这表示dictionary内keys之间用“,”隔开,而KEY和value之间用“:”隔开。
default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError.
sort_keys:将数据根据keys的值进行排序。
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.

其他参数说明


用于序列化的两个模块

  • json,用于字符串 和 python数据类型间进行转换
  • pickle,用于python特有的类型 和 python的数据类型间进行转换

pickle模块提供了四个功能:dumps、dump(序列化,存)、loads(反序列化,读)、load  (不仅可以序列化字典,列表...可以把python中任意的数据类型序列化

1 import pickle
2 # 用于网络传输的时候会将数据转化为bytes类型
3 dic = {"alex": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
4 print(pickle.dumps(dic))    # b‘x80x03}qx00(Xx04x00x00x00alexqx01]q    bytes字节类型数据
5 print(pickle.loads(pickle.dumps(dic)))  # {‘alex‘: [‘woman‘, ‘man‘, ‘lady_boy‘], ‘age‘: 13, ‘hobby‘: ‘老女人‘}
import pickle
dic = {"alex": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
dic1 = {"alex1": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
dic2 = {"alex2": ["woman", "man", "lady_boy"], "age": 13, "hobby": "老女人"}
with open("files", mode="wb") as f1:    # 因为dump出来的是bytes类型的数据,所以向文件中写入到时候要选择b类型操作
    pickle.dump(dic, f1)        # dump()可多次向一个文件中写入多条数据
    pickle.dump(dic1, f1)
    pickle.dump(dic2, f1)
    pickle.dump(dic2, f1)
    pickle.dump(dic2, f1)
    pickle.dump(dic2, f1)


with open("files", mode="rb") as f2:
    while True:     # 读取的时候当文件句柄为生成器
        try:
            print(pickle.load(f2))
        except EOFError:
            break

  shelve也是python提供给我们的序列化工具,比pickle用起来更简单一些。
  shelve只提供给我们一个open方法,是用key来访问的,使用起来和字典类似。

 1 import shelve
 2 f = shelve.open(shelve_file)
 3 f[key] = {int:10, float:9.5, string:Sample data}  #直接对文件句柄操作,就可以存入数据
 4 f.close()
 5 
 6 import shelve
 7 f1 = shelve.open(shelve_file)
 8 existing = f1[key]  #取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报错
 9 f1.close()
10 print(existing)

  这个模块有个限制,它不支持多个应用同一时间往同一个DB进行写操作。所以当我们知道我们的应用如果只进行读操作,我们可以让shelve通过只读方式打开DB

1 import shelve
2 f = shelve.open(shelve_file, flag=r)
3 existing = f[key]
4 f.close()
5 print(existing)

  由于shelve在默认情况下是不会记录待持久化对象的任何修改的,所以我们在shelve.open()时候需要修改默认参数,否则对象的修改不会保存。

import shelve
f1 = shelve.open(shelve_file)
print(f1[key])
f1[key][new_value] = this was not here before
f1.close()

f2 = shelve.open(shelve_file, writeback=True)
print(f2[key])
f2[key][new_value] = this was not here before
f2.close()

  writeback方式有优点也有缺点。优点是减少了我们出错的概率,并且让对象的持久化对用户更加的透明了;但这种方式并不是所有的情况下都需要,首先,使用writeback以后,shelf在open()的时候会增加额外的内存消耗,并且当DB在close()的时候会将缓存中的每一个对象都写入到DB,这也会带来额外的等待时间。因为shelve没有办法知道缓存中哪些对象修改了,哪些对象没有修改,因此所有的对象都会被写入。

 

















以上是关于python-fullstack-s13-day24-python模块部分的主要内容,如果未能解决你的问题,请参考以下文章

python-fullstack-s13-day04-python基础

python-fullstack-s13-day09-python基础

python-fullstack-s13-day03-python基础

python-fullstack-s13-day02-python基础

python-fullstack-s13-day24-python模块部分

python-fullstack-s13-day07-python基础