Built-in module: time, datetime, random, json, pickle, os, sys, hashlib, collections, re

1.time module

import time
time.time() # 时间戳  浮点数
time.sleep() # 睡眠
time.gmtime()/time.localtime() #结构化时间 数据类型是命名元祖
time.strftime('格式化','结构化时间')
time.strptime('字符串','格式化')
time.mktime('结构化时间') #结构化时间转时间戳
# 将时间戳转换成字符串时间
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime(1564028611.631374)))
# 将字符串时间转换成时间戳
print(time.mktime(time.strptime("2024-3-16 12:30:30","%Y-%m-%d %H:%M:%S")))

img

2. datetime

1. Get the current date and time

form datetime import datetime
print(datetime.now())
print(datetime(2018,5,20,13,14)) #获取指定日期和时间

2. datetime and timestamp conversion

form datetime import datetime
print(datetime.now().timestamp()) # 转时间戳
import time
from datetime import datetime
print(datetime.fromtimestamp(time.time()))

3. str with datetime conversion

print(datetime.strptime("2019-10-10 22:23:24","%Y-%m-%d %H:%M:%S")) # 将字符串转换成datetime
print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) # 将datetime转换成字符串
print(str(datetime.now()))

4. datetime subtraction

from datetime import datetime, timedelta
print(datetime.now() + timedelta(days=1))
print(datetime.now() - timedelta(hours=3))

3. random random module

import random
print(random.random()) # 0~1之间的浮点数
print(random.uniform(1,10)) #1~10之间的浮点数
print(random.randint(1,50)) #1~50之间的整数(闭区间)
print(random.randrange(1,5,2)) #(起始,终止,步长)
print(random.choice([1,2,3,4,5,]))#随机选择一个
print(random.choices([1,2,3,4,5,],k=2))#随机选两个,会有重复数字,列表形式
print(random.sample((1,2,3,4,5),k=2))#随机选两个,不会有重复数字,列表形式
lst = [1,2,3,4,5,6,7,8,9,0]
random.shuffle(lst) # 打乱顺序 
print(lst)

Randomly generated codes:

import random
def y_code():
    code = ''
    for i in range(4):
        num = chr(random.randint(48,57))
        # 取数字
        alf = chr(random.randint(97,122))
        # 取小写字母
        alf_upper = chr(random.randint(65,90))
        # 取大写字母
        s = str(random.choice([num,alf,alf_upper]))
        code = ''.join([code,s])
    return code
print(y_code())

4. The sequencing module

Converting a data structure (such as dictionaries, lists), etc. as a particular sequence (string or bytes) of the process is called serialization.

Main purpose: read and write data files, network traffic.

4.1 json module

json module data structure is converted to meet the conditions of particular string, and may also deserialize restored back.

json serialization support only part of the data structure Python: dict, list, tuple, str, int, float, True, False, None

For network transmission: dumps, loads

Used to write the document read: dump, load

4.1.1 dumps、loads

1. The conversion dictionary into a string type Type
import json
dic = {'key1':'1','key2':2}
str_dic = json.dumps(dic)
print(str_dic,type(str_dic))  # 字符串
2. Converts a string into a dictionary type dictionary type
dic1 = json.loads(str_dic)
print(dic1,type(dic1))
3. Also supports the type of list
import json
lit = [1,2,"3",4]
str_lst = json.dumps(lit)
print(str_lst,type(str_lst))
lst = json.loads(str_lst)
print(lst,type(lst))

4.1.2 dump、load

Converting the object into a string 1. speaking written to a file
dic = {'key1':1}
with open('a1.txt','a',encoding='utf-8')as f:
    json.dump(dic,f)  
2. convert the file into a string type of dictionary dictionary
with open('a1.txt','r',encoding='utf-8')as f1:
    dic1 = json.load(f1)
print(dic1)

4.1.3 json sequence of a plurality of data storage to the same file

with open('a1.txt','r',encoding='utf-8')as f1:
    for i in f1:
        lst = json.loads(i)
        print(lst)

4.2 pickle module

The pickle module is Python all data structures and the like and the object type is converted into bytes, then the reduction may also be deserialized back.

The pickle module are sequences recognized only Python language module.

For network transmission: dumps, loads

Used to write the document read: dump, load

4.2.1 dumps、loads

import json
dic = {'key1':'1','key2':2}
str_dic = pickle.dumps(dic)
print(str_dic,type(str_dic))  # 类似字节
dic1 = pickle.loads(str_dic)
print(dic1,type(dic1))

4.2.2 dump、load

with open('a1.txt','ab')as f:
    pickle.dump(dic,f)
with open('a1.txt','rb')as f1:
    dic1 = pickle.load(f1)
print(dic1)

4.2.3 pickle sequence of stored data to a plurality of file

import pickle
dic = {"1":2}
f = open("info","wb")
s = "\n".encode("utf-8")
f.write(pickle.dumps(dic)+ s)
f.write(pickle.dumps(dic)+ s)
f.write(pickle.dumps(dic)+ s)
f.close()

f1 = open("info","rb")
for i in f1:
    print(pickle.loads(i))

5. os module

Os module is an interface to interact with the operating system

os module four groups:

1. Working Directory

import os
print(os.getcwd()) # 当前工作路径  ***
os.chdir("F:\s24\day06") # 路径切换 ***
print(os.curdir)  # 当前目录
print(os.pardir)  # 当前目录的父级目录

2. Folder

import os
os.mkdir("ttt") # 创建一个文件夹  ***
os.rmdir("ttt") # 删除一个文件夹,若不为空则无法删除,报错  ***
os.makedirs("ttt/sss/ddd/ee")  # 递归创建文件夹     ***
os.removedirs("ttt/sss/ddd/ee")  # 递归删除文件夹,若为空删除,不为空不删除   ***
print(os.listdir(r"F:\s24\day17"))  # 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印***

3. File

import os
os.remove()  # 删除一个文件  ***
os.rename("oldname","newname")  # 重命名文件/目录  ***

4. Path

import os
print(os.path.abspath(r"01 今日内容.py"))  # 通过相对路径获取绝对路径  ***
print(os.path.split(os.path.abspath(r"01 今日内容.py")))  #将路径以最后一个\切割(路径,文件名)
print(os.path.dirname(r"F:\s24\day17\01 今日内容.py"))  # 获取路径 ***
print(os.path.basename(r"F:\s24\day17\01 今日内容.py")) # 获取文件名 ***
print(os.path.exists(r"F:\s24\day17\01 今日内容.py"))  # 判断这个路径是否存在  ***
print(os.path.isdir(r"F:\s24\day17"))     # 判断是不是路径  ***
print(os.path.isfile(r"01 今日内容.py"))  # 判断是不是文件  ***
print(os.path.isabs(r"F:\s24\day17\01 今日内容.py"))     # 判断是不是绝对路径 ***
print(os.path.join("D:\\\\","ttt","bbb"))                # 路径拼接 *****
print(os.path.getatime(r"F:\s24\day17\04 序列化.py"))      # 最后的修改时间
print(os.path.getctime(r"F:\s24\day17\04 序列化.py"))      # 最后的访问时间
print(os.path.getmtime(r"F:\s24\day17\04 序列化.py"))      # 最后的访问时间
print(os.path.getsize(r"F:\s24\day09"))                       # 获取当前文件的大小   ***

6. sys module

Sys module is an interface to interact with the python interpreter

import sys
if sys.argv[-1] == "alex":
    print("dsb")
else:
    print("cjb")
print(sys.argv[-1])  
# 当前文件运行['F:/s24/day17/06 sys.py'] ***
import sys
print(sys.exit(1))   #退出程序,正常退出时exit(0),错误退出sys.exit(1)
print(sys.version)  # 获取解释器版本
print(sys.path)     # 添加自定义模块查找路径 *****
print(sys.platform)   # *** 区分操作系统然后进行相关逻辑操作

7. hashlib module

Encryption and checksum

MD5, sha1, sha256, SHA512
1. As long as the same plaintext ciphertext is the same
2. As long as the plaintext is not the same ciphertext is not the same
3. Can not reflexive inverse (can not be decrypted) - md5 Chinese people break

Encryption:
1. encrypted content
2. encrypting content to be converted into byte

import hashlib
md5 = hashlib.md5()  # 加密初始化
md5.update("alex123".encode("utf-8"))
print(md5.hexdigest())

Salt (solid / dynamic)

import hashlib
md5 = hashlib.md5("alex".encode("utf-8"))  
md5.update("alex123".encode("utf-8"))
print(md5.hexdigest())

8. collections module

1.namedtuple generation can use the name to access the content of the element tuple

from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(p)
# Point(x=1, y=2)

2.deque: deque, from the other side can be quickly and additional objects Release

from collections import deque
q = deque(['a', 'b', 'c'])
q.append('x')
q.appendleft('y')
print(q)
# deque(['y', 'a', 'b', 'c', 'x'])

Queue: FIFO

Stack: last out

3.OrderedDict: ordered dictionary

od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
print(od)
# OrderedDict([('a', 1), ('b', 2), ('c', 3)])

4.defaultdict: Dictionary with default values

from collections import defaultdict
values = [11, 22, 33,44,55,66,77,88,99,90]
my_dict = defaultdict(list)
for value in  values:
    if value>66:
        my_dict['k1'].append(value)
    else:
        my_dict['k2'].append(value)

5.Counter: a counter for counting the main

from collections import Counter
c = Counter('abcdeabc')
print(c)
# Counter({'a': 2, 'b': 2, 'c': 2, 'd': 1})

9. re module (regex)

# \w 匹配字母(包含中文)或数字或下划线 
import re
name = "宝元-meet_123 "
print(re.findall("\w",name))
# ['宝', '元', 'm', 'e', 'e', 't', '_', '1', '2', '3']
# \W 匹配非字母(包含中文)或数字或下划线 
# \s 匹配任意的空白符 
# \S 匹配任意非空白符 
# \d 匹配数字 
# \D 匹配非数字 
# \A 从字符串开头匹配 
# \Z 匹配字符串的结束,如果是换行,只匹配到换行前的结果 
# \n 匹配一个换行符 
# \t 匹配一个制表符 
# ^ 匹配字符串的开始 
# $ 匹配字符串的结尾 
# . 匹配任意字符,除了换行符,当re.DOTALL标记被指定时,则可以匹配包括换行符的任意字符。 
import re
name = "宝元-meet_123\t \n"
print(re.findall(".",name))
# ['宝', '元', '-', 'm', 'e', 'e', 't', '_', '1', '2', '3', '\t', ' ']
print(re.findall(".",name,re.DOTALL))
# ['宝', '元', '-', 'm', 'e', 'e', 't', '_', '1', '2', '3', '\t', '\n']
# [...] 匹配字符组中的字符 
print(re.findall("[a-z]",s))
# [^...] 匹配除了字符组中的字符的所有字符 
# * 匹配0个或者多个左边的字符。 
# + 匹配一个或者多个左边的字符。 
# ? 匹配0个或者1个左边的字符,非贪婪方式。 
# {n} 精准匹配n个前面的表达式。 
# {n,m} 匹配n到m次由前面的正则表达式定义的片段,贪婪方式 
# a|b 匹配a或者b 
# () 匹配括号内的表达式,也表示一个组 
import re
print(re.findall('(.*?)_sb', 'alex_sb wusir_sb 日天_sb'))
# ['alex', ' wusir', ' 日天']
print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>')
# ['http://www.baidu.com']

1.findall all found, returns a list

2.search from anywhere in the string to find a match stopped, returns an object. For a matching content must be used .group () were acquired

3.match only at the beginning of the string matching

4.split partition can be separated in any delimiter

5.sub replacement

6.compile defined matching rules

obj = re.compile("\w")
print(obj.findall("meet_宝元_常鑫垃圾"))

7.finditer Returns an iterator

import re
g = re.finditer('al',"alex_alsb,al22,aladf")
print(next(g).group())
print([i.group() for i in g])
# al
# ['al','al','al']

Guess you like

Origin www.cnblogs.com/lav3nder/p/11801807.html