Home >Backend Development >Python Tutorial >python module sys module and serialization module

python module sys module and serialization module

巴扎黑
巴扎黑Original
2017-09-15 10:44:331674browse

The following editor will bring you an article on the sys module and serialization module of Python modules (explanation with examples). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor to take a look

sys module

The sys module is an interface for interacting with the python interpreter


sys.argv   命令行参数List,第一个元素是程序本身路径
sys.exit(n)  退出程序,正常退出时exit(0),错误退出sys.exit(1)
sys.version  获取Python解释程序的版本信息
sys.path   返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform  返回操作系统平台名称

Serialization module

The purpose of serialization:

Use the Define object persistence

Transfer objects from one place to another

Make the program more maintainable

python module sys module and serialization module

json


# Json模块提供了四个功能:dumps、dump、loads、load


import json
dic = {'k1':'v1','k2':'v2','k3':'v3'}
str_dic = json.dumps(dic) #序列化:将一个字典转换成一个字符串
print(type(str_dic),str_dic) #<class &#39;str&#39;> {"k3": "v3", "k1": "v1", "k2": "v2"}
#注意,json转换完的字符串类型的字典中的字符串是由""表示的

dic2 = json.loads(str_dic) #反序列化:将一个字符串格式的字典转换成一个字典
#注意,要用json的loads功能处理的字符串类型的字典中的字符串必须由""表示
print(type(dic2),dic2) #<class &#39;dict&#39;> {&#39;k1&#39;: &#39;v1&#39;, &#39;k2&#39;: &#39;v2&#39;, &#39;k3&#39;: &#39;v3&#39;}


list_dic = [1,[&#39;a&#39;,&#39;b&#39;,&#39;c&#39;],3,{&#39;k1&#39;:&#39;v1&#39;,&#39;k2&#39;:&#39;v2&#39;}]
str_dic = json.dumps(list_dic) #也可以处理嵌套的数据类型 
print(type(str_dic),str_dic) #<class &#39;str&#39;> [1, ["a", "b", "c"], 3, {"k1": "v1", "k2": "v2"}]
list_dic2 = json.loads(str_dic)
print(type(list_dic2),list_dic2) #<class &#39;list&#39;> [1, [&#39;a&#39;, &#39;b&#39;, &#39;c&#39;], 3, {&#39;k1&#39;: &#39;v1&#39;, &#39;k2&#39;: &#39;v2&#39;}]


import json
f = open(&#39;json_file&#39;,&#39;w&#39;)
dic = {&#39;k1&#39;:&#39;v1&#39;,&#39;k2&#39;:&#39;v2&#39;,&#39;k3&#39;:&#39;v3&#39;}
json.dump(dic,f) #dump方法接收一个文件句柄,直接将字典转换成json字符串写入文件
f.close()

f = open(&#39;json_file&#39;)
dic2 = json.load(f) #load方法接收一个文件句柄,直接将文件中的json字符串转换成数据结构返回
f.close()
print(type(dic2),dic2)


import json
f = open(&#39;file&#39;,&#39;w&#39;)
json.dump({&#39;国籍&#39;:&#39;中国&#39;},f)
ret = json.dumps({&#39;国籍&#39;:&#39;中国&#39;})
f.write(ret+&#39;\n&#39;)
json.dump({&#39;国籍&#39;:&#39;美国&#39;},f,ensure_ascii=False)
ret = json.dumps({&#39;国籍&#39;:&#39;美国&#39;},ensure_ascii=False)
f.write(ret+&#39;\n&#39;)
f.close()

ensure_ascii关键字参数

pickle

json & pickle

json, used to convert between strings and python data types

pickle, used for python-specific Convert between types and python data types


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


import pickle
dic = {&#39;k1&#39;:&#39;v1&#39;,&#39;k2&#39;:&#39;v2&#39;,&#39;k3&#39;:&#39;v3&#39;}
str_dic = pickle.dumps(dic)
print(str_dic) #一串二进制内容

dic2 = pickle.loads(str_dic)
print(dic2) #字典

import time
struct_time = time.localtime(1000000000)
print(struct_time)
f = open(&#39;pickle_file&#39;,&#39;wb&#39;)
pickle.dump(struct_time,f)
f.close()

f = open(&#39;pickle_file&#39;,&#39;rb&#39;)
struct_time2 = pickle.load(f)
print(struct_time2.tm_year)

shelve


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



import shelve
f = shelve.open(&#39;shelve_file&#39;)
f[&#39;key&#39;] = {&#39;int&#39;:10, &#39;float&#39;:9.5, &#39;string&#39;:&#39;Sample data&#39;} #直接对文件句柄操作,就可以存入数据
f.close()

import shelve
f1 = shelve.open(&#39;shelve_file&#39;)
existing = f1[&#39;key&#39;] #取出数据的时候也只需要直接用key获取即可,但是如果key不存在会报错
f1.close()
print(existing)


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


import shelve
f = shelve.open(&#39;shelve_file&#39;, flag=&#39;r&#39;)
existing = f[&#39;key&#39;]
f.close()
print(existing)


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


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

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



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

The above is the detailed content of python module sys module and serialization module. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn