Module Ⅱ
Serialization module (Key)
Definition: The structure of a data type (list, dict ...) is converted into a sequence specific (special string) process.
Sometimes encountered when the file storage and file transfer, the original data structure into str () is stored in a file, or converted into bytes transmitted over the network, or reading the file receives the original file can not be converted into a data structure . May be the original data type conversion serialization module is a special string that can be converted to any data structure with each other.
Classification serialization module
Sequencing module may be converted to a data structure for a particular sequence (string, bytes) and can also be converted back. Currently, there are three serialization module: json module, pickle module, shevle module
json serialization
Language is recognized as the most commonly used sequences of all, but the limited data structure support python
It can be divided into two pairs of four methods:
dumps, loads mainly used for network transmission, can also be used to access the file
import json dic = {'username': '王大锤', 'password': 123,'status': False} ret = json.dumps(dic)#序列化:将一个字典转换成一个字符串 print(ret,type(ret)) #{"username": "\u738b\u5927\u9524", "password": 123, "status": false} <class 'str'> #注意,json转换完的字符串类型的字典中的字符串是由""表示的 ret_dict = json.loads(ret)#反序列化:将一个字符串格式的字典转换成一个字典 print(ret_dict) #{'username': '王大锤', 'password': 123, 'status': False} #注意,要用json的loads功能处理的字符串类型的字典中的字符串必须由""表示
You can pass parameters:
dic = {'username': '王大锤', 'password': 123,'status': False} ret = json.dumps(dic,ensure_ascii=False,sort_keys=True) print(ret,type(ret)) #{"password": 123, "status": false, "username": "王大锤"} <class 'str'>
Other Parameters: ensure_ascii :, when it is True, when all non-ASCII characters appear as \ uXXXX sequence, just when the dump will ensure_ascii set to False, then credited the json Chinese to display properly . Separators: Separator, actually (item_separator, dict_separator) a tuple, the default is ( ',', ':'); between which is represented by the dictionary keys "," separated, and the value of KEY Room with ":" separated. sort_keys: The data is sorted according to the value of the keys.
Access file:
import json dic = {'username': '王大锤', 'password': 123,'status': False} s_dict = json.dumps(dic) with open('jsonlx.json',encoding='utf-8',mode='w') as f1: f1.write(s_dict) with open('jsonlx.json',encoding='utf-8') as f2: content = f2.read() print(json.loads(content))
dump, load for file access
import json f = open('json_file.json','w') dic = {'k1':'v1','k2':'v2','k3':'v3'} json.dump(dic,f) #dump方法接收一个文件句柄,直接将字典转换成json字符串写入文件 f.close() # json文件也是文件,就是专门存储json字符串的文件。 f = open('json_file.json') dic2 = json.load(f) #load方法接收一个文件句柄,直接将文件中的json字符串转换成数据结构返回 f.close() print(type(dic2),dic2)
Storing a plurality of data files
import json dic1 = {'username': '王大锤', 'password': 123,'status': False} dic2 = {'username': 'abc', 'password': 123,'status': False} dic3 = {'username': 'ly', 'password': 123,'status': False} with open('jsonmore.json',encoding='utf-8',mode='a') as f1: f1.write(f'{json.dumps(dic1)}\n{json.dumps(dic2)}\n{json.dumps(dic3)}') with open('jsonmore.json',encoding='utf-8') as f1: for line in f1: ret = json.loads(line) print(ret,type(ret))
pickle serialization
dumps, loads can only be used for network transmission
import pickle l1 = ['wusir', '太白', '小黑', 666] ret = pickle.dumps(l1) print(ret) #b'\x80\x03]q\x00(X\x06\x00\x00\x00\xe5\xb0\x8f\xe9\xbb\x91q\x01M\x9a\x02e.' l2 = pickle.loads(ret) print(l2,type(l2)) ['小黑', 666] <class 'list'>
dump, load can only be used for file access
import pickle l1 = ['wusir', '太白', '小黑', 666] with open('pickle练习.pickle',mode='wb') as f1: pickle.dump(l1,f1) with open('pickle练习.pickle', mode='rb') as f1: ret = pickle.load(f1) print(ret,type(ret))
More data is written
# 多个数据写入文件 l1 = ['wusir', '太白', '小黑1', 666] l2 = ['wusir', '太白', '小黑2', 666] l3 = ['wusir', '太白', '小黑3', 666] with open('pickle练习1.pickle',mode='wb') as f1: pickle.dump(l1,f1) pickle.dump(l2,f1) pickle.dump(l3,f1) with open('pickle练习1.pickle', mode='rb') as f1: ret1 = pickle.load(f1) ret2 = pickle.load(f1) ret3 = pickle.load(f1) print(ret1,ret2,ret3)
os module
And the working directory (the current directory, parent directory) related
import os print(os.getcwd()) # 获取当前工作目录,即当前python脚本工作的目录路径 ** os.chdir(r'D:\s23\day9')# 改变当前脚本工作目录;相当于shell下cd ** os.curdir # 返回当前目录: ('.') ** os.pardir # 获取当前目录的父目录字符串名:('..') **
And related folders ***
os.makedirs('dirname1/dirname2') #可生成多层递归目录 *** os.removedirs('dirname1') #若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推 *** os.mkdir('dirname') #生成单级目录;相当于shell中mkdir dirname *** os.rmdir('dirname') #删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname *** os.listdir('dirname') #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印 **
And related documents
os.remove() #删除一个文件 *** os.rename("oldname","newname") #重命名文件/目录 *** os.stat('path/filename') #获取文件/目录信息 **
And path-related
os.path.abspath(path) #返回path规范化的绝对路径 *** os.path.split(path) #将path分割成目录和文件名二元组返回 *** os.path.dirname(path) #返回path的目录。其实就是os.path.split(path)的第一个元素 ** os.path.basename(path) #返回path最后的文件名。如何path以/或\结尾,那么就会返回空值,即os.path.split(path)的第二个元素。 ** os.path.exists(path) #如果path存在,返回True;如果path不存在,返回False *** os.path.isabs(path) #如果path是绝对路径,返回True ** os.path.isfile(path) #如果path是一个存在的文件,返回True。否则返回False *** os.path.isdir(path) #如果path是一个存在的目录,则返回True。否则返回False *** os.path.join(path1[, path2[, ...]]) #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略 *** os.path.getatime(path) #返回path所指向的文件或者目录的最后访问时间 ** os.path.getmtime(path) #返回path所指向的文件或者目录的最后修改时间 ** os.path.getsize(path) #返回path的大小 ***
sys module
Sys module is an interface to interact with the python interpreter
sys.argv #命令行参数List,第一个元素是程序本身路径 sys.exit(n) #退出程序,正常退出时exit(0),错误退出sys.exit(1) sys.version #获取Python解释程序的版本信息 sys.path #返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值 *** sys.platform #返回操作系统平台名称
hashlib module
Definition: an encryption module, digest algorithms, hashing algorithms, etc., he is a collection of a bunch of encryption algorithms
How hashlib encryption
- One type of data bytes encrypted by hashlib, returns the length of a hexadecimal numbers, etc.
- Process Irreversible
- The same type of data bytes identical absolute numbers by the same encryption method obtained
- Are not the same type of data bytes are not absolutely the same encryption method by the same numbers obtained
use
File encryption, md5 encryption level is the lowest, sha series of high safety factor, highly time-consuming.
import hashlib md5 = hashlib.md5() md5.update('123456'.encode('utf-8')) print(md5.hexdigest()) # 'e10adc3949ba59abbe56e057f20f883e'
Plus a fixed salt
ret = hashlib.md5('xxx公司'.encode('utf-8')) ret.update('123456'.encode('utf-8')) s = ret.hexdigest() print(s,type(s)) # 1ef97f2f10b2ba2dc273a9641cd713d2 <class 'str'>
Plus dynamic of salt
username = input('输入用户名:').strip() password = input('输入密码').strip() ret = hashlib.md5(username[::2].encode('utf-8')) ret.update(password.encode('utf-8')) s = ret.hexdigest() print(s)
File consistency check
import hashlib def md5_file(path): ret = hashlib.md5() with open(path,mode='rb') as f1: while 1: content = f1.read(1024) if content: ret.update(content) else: return ret.hexdigest()
time module
Indicates that the card: a time stamp (timestamp), a formatted string time (Format String), tuple (struct_time)
Timestamp
import time print(time.time()) # 1561713996.1833062
Time string
import time print(time.strftime('%y-%m-%d %H:%M:%S')) # 19-06-28 17:27:27
Structured time
localtime will convert a timestamp to the current time zone struct_time
import time timestamp = time.time() st = time.localtime(timestamp) print(st) # time.struct_time(tm_year=2019, tm_mon=6, tm_mday=28, tm_hour=17, tm_min=34, tm_sec=33, tm_wday=4, tm_yday=179, tm_isdst=0)
Formatting time ----> timestamp
ft = time.strftime('%Y/%m/%d %H:%M:%S') st = time.strptime(ft,'%Y/%m/%d %H:%M:%S') print(st) # 转化为结构化时间 timestamp = time.mktime(st) print(timestamp) # 转化为时间戳 # time.struct_time(tm_year=2019, tm_mon=6, tm_mday=28, tm_hour=17, tm_min=35, tm_sec=55, tm_wday=4, tm_yday=179, tm_isdst=-1) #1561714737.0
Timestamp ---> Structured time
t = time.time() st = time.localtime(t) print(st) # 转化为结构化时间 ft = time.strftime('%Y/%m/%d %H:%M:%S',st) print(ft) # 转化为格式化时间 # time.struct_time(tm_year=2019, tm_mon=6, tm_mday=28, tm_hour=17, tm_min=42, tm_sec=55, tm_wday=4, tm_yday=179, tm_isdst=0) # 19-06-28 17:42:55
datetime module
# datatime模块 import datetime now_time = datetime.datetime.now() # 现在的时间 # 只能调整的字段:weeks days hours minutes seconds print(datetime.datetime.now() + datetime.timedelta(weeks=3)) # 三周后 print(datetime.datetime.now() + datetime.timedelta(weeks=-3)) # 三周前 current_time = datetime.datetime.now() # 可直接调整到指定的 年 月 日 时 分 秒 等 print(current_time.replace(year=1977)) # 直接调整到1977年 print(current_time.replace(month=1)) # 直接调整到1月份 print(current_time.replace(year=1989,month=4,day=25)) # 1989-04-25 18:49:05.898601 # 将时间戳转化成时间 print(datetime.date.fromtimestamp(1232132131)) # 2009-01-17
random module
>>> import random #随机小数 >>> random.random() # 大于0且小于1之间的小数 0.7664338663654585 >>> random.uniform(1,3) #大于1小于3的小数 1.6270147180533838 #随机整数 >>> random.randint(1,5) # 大于等于1且小于等于5之间的整数 >>> random.randrange(1,10,2) # 大于等于1且小于10之间的奇数 #随机选择一个返回 >>> random.choice([1,'23',[4,5]]) # #1或者23或者[4,5] #随机选择多个返回,返回的个数为函数的第二个参数 >>> random.sample([1,'23',[4,5]],2) # #列表元素任意2个组合 [[4, 5], '23'] #打乱列表顺序 >>> item=[1,3,5,7,9] >>> random.shuffle(item) # 打乱次序 >>> item [5, 1, 3, 7, 9] >>> random.shuffle(item) >>> item [5, 9, 7, 1, 3]