Python学习-day5 常用模块

时间:2022-03-04 05:14:00

day5主要是各种常用模块的学习

  • time &datetime模块
  • random
  • os
  • sys
  • shutil
  • json & picle
  • shelve
  • xml处理
  • yaml处理
  • configparser
  • hashlib
  • subprocess
  • logging模块
  • re正则表达式

time & datetime模块

#Authon Ivor

import time
#打印时间戳
print(time.time())
#打印当前时间的时间对象格式
print(time.localtime()) #字符串转换时间对象
print(time.strptime("2016-02-02 15:52:20","%Y-%m-%d %H:%M:%S"))
#时间对象转换字符串
print(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())) #时间对象转换时间戳
print(time.mktime(time.localtime()))
#时间戳转换时间对象
print(time.gmtime(time.time())) import datetime #打印当前日期时间
print(datetime.datetime.now())
#从时间戳转换成年月日格式
print(datetime.date.fromtimestamp(time.time()))
#从时间戳转换成年月日时间格式
print(datetime.datetime.fromtimestamp(time.time()))
#对当前时间进行加减运算
print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(hours=-3))
#直接进行时间替换
t = datetime.datetime.now()
print(t.replace(year=2012,month=12,day=24,hour=0,minute=0,second=0))

random模块

#Authon Ivor

import random
for i in range(10):
print(random.random())
print(random.randint(1,5))
print(random.randrange(1,5))

shutil 模块

#Authon Ivor

import shutil
#拷贝文件对象
f1 = open("random mod.py")
f2 = open("random_new.py","w")
shutil.copyfileobj(f1,f2,length=1) #拷贝文件,文件和权限,包括copyfile和copymode
shutil.copy("random mod.py","random_new.py")
#拷贝文件和状态信息,包括copyfile和copystat
shutil.copy2("random mod.py","random_new.py") #拷贝文件,不包括权限
shutil.copyfile("random mod.py","random_new.py")
#仅拷贝文件权限,仅拷贝权限。内容、组、用户均不变
shutil.copymode("random mod.py","random_new.py")
#拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copystat("random mod.py","random_new.py") #拷贝文件目录,递归拷贝
shutil.copytree()
#递归删除
shutil.rmtree()
#移动文件
shutil.move() shutil.make_archive()
#base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
# 如:www =>保存至当前路径
# 如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
# •format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
# •root_dir: 要压缩的文件夹路径(默认当前目录)
# •owner: 用户,默认当前用户
# •group: 组,默认当前组
# •logger: 用于记录日志,通常是logging.Logger对象 import zipfile # 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close() # 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close() # zipfile 压缩解压 import tarfile # 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
tar.close() # 解压
tar = tarfile.open('your.tar','r')
tar.extractall() # 可设置解压地址
tar.close()

json & pickle 模块

#Authon Ivor

import json
# info = {
# "name":"Ivor",
# "age":"27",
#} info = ["a","b","c","f","f","g"]
with open("jsondump.json","w") as f:
f.write(json.dumps(info)) with open("jsondump.json","w") as f:
json.dump(info,f) #Authon Ivor import json
info = {
"name":"Ivor",
"age":"",
} with open("jsondump.json","r") as f:
a = json.loads(f.read()) with open("jsondump.json","r") as f:
b = json.load(f) print(a,b) #Authon Ivor
import pickle def sayhi():
print("Hello World!") info = {
"name":"Ivor",
"func":sayhi
} with open("pickle.pickle","wb") as f:
f.write(pickle.dumps(info)) with open("pickle.pickle", "wb") as f:
pickle.dump(info,f) #Authon Ivor import pickle def sayhi():
print("Hello World!") with open("pickle.pickle","rb") as f:
pickle.loads(f.read()) with open("pickle.pickle","rb") as f:
pickle.load(f)

shelve 模块

#Authon Ivor
import shelve d = shelve.open('shelve_test') # 打开一个文件 class Test(object):
def __init__(self, n):
self.n = n t1 = Test("")
t2 = Test("ivor")
name = ["alex","ivor"] d["name"] = name
d["t1"] = t1
d["t2"] = t2 d.close()

xml处理模块

#Authon Ivor

import xml.etree.ElementTree as et

tree = et.parse("test.xml")
root = tree.getroot()
print(root.tag) #遍历xml文档
for child in root:
print(child.tag,child.attrib)
for i in child:
print(i.tag,i.text) #只遍历year节点
for node in root.iter("year"):
print(node.tag,node.text) #修改
for node in root.iter("year"):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updates","yes") tree.write("test2.xml") #删除
for country in root.iter("country"):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country) tree.write("test3.xml") 创建xml tree = et.Element("Tree")
stu = et.SubElement(tree,"Stu1")
name = et.SubElement(stu,"name",attrib={"enrolled":"yes"})
name.text = "ivor"
age = et.SubElement(stu,"age",attrib={"checked":"no"})
age.text = ""
stu2 = et.SubElement(tree,"Stu2")
name = et.SubElement(stu2,"name",attrib={"enrolled":"yes"})
name.text = "dark"
age = et.SubElement(stu2,"age",attrib={"checked":"no"})
age.text = "" et = et.ElementTree(tree)
et.write("test4.xml",encoding="utf-8",xml_declaration=True)

ConfigParser模块

#Authon Ivor

import configparser
#新增
config = configparser.ConfigParser()
config["DEFAULT"] = {
"port":5506,
"enable":"YES"
}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
#修改
config["DEFAULT"]["enable"] = "NO"
a = config.sections() print(a) #写入
with open("conf.ini","w") as f:
config.write(f) #读取
config.read("conf.ini")
print(config.sections())
print(config.items())
print(config.keys())
print(config.values()) # ########## 读 ########## # secs = config.sections() # print secs # options = config.options('group2') # print options # item_list = config.items('group2') # print item_list # val = config.get('group1','key') # val = config.getint('group1','key') # ########## 改写 ########## # sec = config.remove_section('group1') # config.write(open('i.cfg', "w")) # sec = config.has_section('wupeiqi') # sec = config.add_section('wupeiqi') # config.write(open('i.cfg', "w")) # config.set('group2','k1',11111) # config.write(open('i.cfg', "w")) # config.remove_option('group2','age') # config.write(open('i.cfg', "w"))

hashlib模块

#Authon Ivor
import hashlib m = hashlib.md5()
m.update(b"Hello")
m.update(b"It's me")
print(m.digest())
m.update(b"It's been a long time since last time we ...")
print(m.digest()) # 2进制格式hash
print(len(m.hexdigest())) # 16进制格式hash
'''
def digest(self, *args, **kwargs): # real signature unknown """ Return the digest value as a string of binary data. """ pass
def hexdigest(self, *args, **kwargs): # real signature unknown """ Return the digest value as a string of hexadecimal digits. """ pass
'''
import hashlib
# ######## md5 ########
hash = hashlib.md5()
hash.update('admin')
print(hash.hexdigest())
# ######## sha1 ########
hash = hashlib.sha1()
hash.update('admin')
print(hash.hexdigest())
# ######## sha256 ########
hash = hashlib.sha256()
hash.update('admin')
print(hash.hexdigest())
# ######## sha384 ########
hash = hashlib.sha384()
hash.update('admin')
print(hash.hexdigest())
# ######## sha512 ########
hash = hashlib.sha512()
hash.update('admin')
print(hash.hexdigest())

Subprocess模块

#Authon Ivor

import subprocess

#执行命令,返回命令执行状态 , 0 or 非0
retcode = subprocess.call(["ls", "-l"]) #执行命令,如果命令结果为0,就正常返回,否则抛异常
subprocess.check_call(["ls", "-l"]) #接收字符串格式命令,返回元组形式,第1个元素是执行状态,第2个是命令结果
subprocess.getstatusoutput('ls /bin/ls')
(0, '/bin/ls') #接收字符串格式命令,并返回结果
subprocess.getoutput('ls /bin/ls')
'/bin/ls' #执行命令,并返回结果,注意是返回结果,不是打印,下例结果返回给res
res=subprocess.check_output(['ls','-l'])
res
b'total 0\ndrwxr-xr-x 12 alex staff 408 Nov 2 11:05 OldBoyCRM\n' #上面那些方法,底层都是封装的subprocess.Popen
p = subprocess.Popen("df -h|grep disk",stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
p.stdout.read() # 可用参数:
# •args:shell命令,可以是字符串或者序列类型(如:list,元组)
# •bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
# •stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
# •preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
# •close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
# 所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
# •shell:同上
# •cwd:用于设置子进程的当前目录
# •env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
# •universal_newlines:不同系统的换行符不同,True -> 同意使用 \n
# •startupinfo与createionflags只在windows下有效
# 将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等

logging模块

#Authon Ivor
import logging logging.basicConfig(filename="access.log",level=logging.INFO,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') # 日志格式
#
# %(name)s
# Logger的名字
#
# %(levelno)s
# 数字形式的日志级别
#
# %(levelname)s
# 文本形式的日志级别
#
# %(pathname)s
# 调用日志输出函数的模块的完整路径名,可能没有
#
# %(filename)s
# 调用日志输出函数的模块的文件名
#
# %(module)s
# 调用日志输出函数的模块名
#
# %(funcName)s
# 调用日志输出函数的函数名
#
# %(lineno)d
# 调用日志输出函数的语句所在的代码行
#
# %(created)f
# 当前时间,用UNIX标准的表示时间的浮点数表示
#
# %(relativeCreated)d
# 输出日志信息时的,自Logger创建以来的毫秒数
#
# %(asctime)s
# 字符串形式的当前时间。默认格式是 “2003 - 07 - 08 16: 49:45, 896”。逗号后面的是毫秒
#
# %(thread)d
# 线程ID。可能没有
#
# %(threadName)s
# 线程名。可能没有
#
# %(process)d
# 进程ID。可能没有
#
# %(message)s
# 用户输出的消息

re模块 

#Authon Ivor
def addition(a):
return str(round(float(a[0].strip())+float(a[1].strip()),5))
def subtraction(a):
return str(round(float(a[0].strip())-float(a[1].strip()),5))
def multiplication(a):
return str(round(float(a[0].strip())*float(a[1].strip()),5))
def division(a):
return str(round(float(a[0].strip())/float(a[1].strip()),5)) import re num = '''
请输入运算式:
1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )
>>>
'''
a = input(num)
a = a.replace(" ","")
while True:
b = re.search(r'\([^()]+\)',a)
if b:
c = re.search('\d+\.?\d*/(\+|\-)?\d+\.?\d*',b.group())
if c:
d = division(c.group().split("/"))
a = a.replace(c.group(),d)
continue
c = re.search('\d+\.?\d*\*(\+|\-)?\d+\.?\d*',b.group())
if c:
d = multiplication(c.group().split("*"))
a = a.replace(c.group(),d)
continue
c = re.search('\d+\.?\d*\-\d+\.?\d*',b.group())
if c:
d = subtraction(c.group().split("-"))
a = a.replace(c.group(),d)
continue
c = re.search('\d+\.?\d*\+\d+\.?\d*',b.group())
if c:
d = addition(c.group().split("+"))
a = a.replace(c.group(),d)
continue
c = re.search('\d+\.?\d*(\+|\-){2}\d+\.?\d*',b.group())
if c:
if "+-" in c.group():
a = a.replace(c.group(),c.group().replace("+-","-"))
if "--" in c.group():
a = a.replace(c.group(),c.group().replace("--","+"))
if "-+" in c.group():
a = a.replace(c.group(),c.group().replace("-+","-"))
if "++" in c.group():
a = a.replace(c.group(),c.group().replace("++","+"))
continue
if b and not c:
a = a.replace(b.group(),b.group().strip("()"))
continue
else:
if "+-" in a:
a = a.replace("+-","-")
if "--" in a:
a = a.replace("--","+")
if "-+" in a:
a = a.replace("-+","-")
if "++" in a:
a = a.replace("++","+")
b = re.search('\d+\.?\d*/(\+|\-)?\d+\.?\d*', a)
if b:
c = division(b.group().split("/"))
a = a.replace(b.group(),c)
continue
b = re.search('\d+\.?\d*\*(\+|\-)?\d+\.?\d*',a)
if b:
c = multiplication(b.group().split("*"))
a = a.replace(b.group(),c)
continue
b = re.search('\d+\.?\d*\-\d+\.?\d*', a)
if b:
c = subtraction(b.group().split("-"))
a = a.replace(b.group(),c)
continue
b = re.search('\d+\.?\d*\+\d+\.?\d*', a)
if b:
c = addition(b.group().split("+"))
a = a.replace(b.group(),c)
continue
print(a)
exit()