Python 常用模块详解


一、模块

1、定义:
模块:用来从逻辑上组织Python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test)
包:用来从逻辑上组织模块的,本质就一个目录(必须带有一个__init__.py文件)

2、导入方法
import module_name
import module1_name,module2_name
from module_name import *
from module_name import m1,m2,m3
from module_1 import logger as logger_1
3、import本质(路径搜索和搜索路径)
导入模块的本质就是把python文件解释一遍(import test test='test.py all code')
(from test imort m1 m1='code')
import module_name----->module_name.py------>module_name.py的路径----->sys.path
导入包的本质就是执行该包下的__init__.py文件
4、导入优化
from module_test import test
5、模块的分类:
a:标准库
b:开源模块
c:自定义模块

二、time和datetime模块

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import time

print(time.process_time())  # 返回处理器时间
print(time.altzone)     # 返回与utc时间的时间差,以秒计算
print(time.asctime())   # 返回时间格式"Thu Sep  5 15:41:24 2019"
print(time.localtime()) # 返回本地时间的stuct time对象格式
print(time.gmtime(time.time()-800000))  # 返回utc时间的stuct时间对象格式

print(time.asctime(time.localtime()))   # 返回时间格式"Thu Sep  5 15:44:17 2019"
print(time.ctime()) # 返回时间格式"Thu Sep  5 15:44:58 2019"

# 日期字符串 转成 时间戳
string_2_struct = time.strptime("2019/09/05","%Y/%m/%d")    #将 日期字符串 转成 struct时间对象格式
print(string_2_struct)

struct_2_stamp = time.mktime(string_2_struct)   # 将struct时间对象转成时间戳
print(struct_2_stamp)

# 将时间戳转为字符串格式
print(time.gmtime(time.time()-86640))   # 将utc时间戳转成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) )    # 将utc struct_time格式转成指定的字符串模式

# 时间加减
import datetime

print(datetime.datetime.now())  # 返回 2019-09-05 15:54:14.382000
print(datetime.date.fromtimestamp(time.time())) # 时间戳直接改成日期格式 2019-09-05
print(datetime.datetime.now() + datetime.timedelta(3))  # 当前时间+3天
print(datetime.datetime.now() + datetime.timedelta(-3))  # 当前时间-3天
print(datetime.datetime.now() + datetime.timedelta(hours=3))  # 当前时间+3小时
print(datetime.datetime.now() + datetime.timedelta(minutes=30))  # 当前时间+30分

c_time = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) # 时间替换

strftime("格式", struct_time) --->"格式化的字符串"
strptime("格式化的字符串", "格式") --->struct_time

>>> time.strftime("%Y-%m:%d %H:%M:%S",time.localtime())
'2019-09:04 11:15:35'

>>> time.strptime('2019-09:04 11:15:35',"%Y-%m:%d %H:%M:%S")
time.struct_time(tm_year=2019, tm_mon=9, tm_mday=4, tm_hour=11, tm_min=15, tm_se
c=35, tm_wday=2, tm_yday=247, tm_isdst=-1

三、random模块

>>> import random
>>> random.random()
0.649073454479157
>>> random.randint(1,3)
3
>>> random.randrange(1,3)
2
>>> random.choice('hello')
'l'
>>> random.choice('hello')
'o'
>>> random.sample('hello',2)
['e', 'l']
>>> random.uniform(1,3)
1.9550232098752554
>>> l = [1,2,3,4,5,6]
>>> l
[1, 2, 3, 4, 5, 6]
>>> random.shuffle(l)
>>> print(l)
[4, 3, 5, 1, 2, 6]

生成随机验证码

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import random
checkcode=''
for i in range(5):
    #i=0
    current=random.randrange(0,5)
    #字母
    if current == i:
        tmp=chr(random.randint(65,90))
    #数字
    else:
        tmp=random.randint(0,9)
    checkcode += str(tmp)
print(checkcode)

Python 常用模块详解

四、OS模块

os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  改变当前脚本工作目录;相当于shell下cd
os.curdir  返回当前目录: ('.')
os.pardir  获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')    可生成多层递归目录
os.removedirs('dirname1')    若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname')    生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname')    删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname')    列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove()  删除一个文件
os.rename("oldname","newname")  重命名文件/目录
os.stat('path/filename')  获取文件/目录信息
os.sep    输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep    输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep    输出用于分割文件路径的字符串
os.name    输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command")  运行shell命令,直接显示
os.environ  获取系统环境变量
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所指向的文件或者目录的最后修改时间

五、sys模块

sys.argv           命令行参数List,第一个元素是程序本身路径
sys.exit(n)        退出程序,正常退出时exit(0)
sys.version        获取Python解释程序的版本信息
sys.maxint         最大的Int值
sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform       返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]

六、shutil模块

高级的文件、文件夹、压缩包 处理模块

shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中,可以部分内容

shutil.copyfile(src, dst)
拷贝文件

shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变

shutil.copystat(src, dst)
拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copy(src, dst)
拷贝文件和权限

shutil.copy2(src, dst)
拷贝文件和状态信息

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件

例如:copytree(source, destination, ignore=ignore_patterns('*.pyc', 'tmp*'))

shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件

shutil.move(src, dst)
递归的去移动文件

shutil.make_archive(base_name, format,...)

创建压缩包并返回文件路径,例如:zip、tar

base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
如:www =>保存至当前路径
如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
root_dir: 要压缩的文件夹路径(默认当前目录)
owner: 用户,默认当前用户
group: 组,默认当前组
logger: 用于记录日志,通常是logging.Logger对象

shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:

import zipfile

# 压缩
z = zipfile.ZipFile('hhh.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解压
z = zipfile.ZipFile('hhh.zip', 'r')
z.extractall()
z.close()
import tarfile

# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/hhh/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/hhh/PycharmProjects/cmdb.zip',
arcname='cmdb.zip')
tar.close()

# 解压
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可设置解压地址
tar.close()

七、json和pickle模块

json序列化1

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# import json
import pickle
def sayhi(name):
    print("hello",name)

info = {
    'name':'alex',
    'age':22,
    'func':sayhi
}
f = open("text.text","wb")
# print(json.dumps(info))
f.write(pickle.dumps(info))

f.close()

json反序列化1

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pickle

def sayhi(name):
    print("hello2",name)

f = open("text.text","rb")
data = pickle.loads(f.read())
print(data["func"]("alex"))

json序列化2

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# import json
import pickle
def sayhi(name):
print("hello",name)

info = {
'name':'alex',
'age':22,
'func':sayhi
}
f = open("text.text","wb")
pickle.dump(info,f)
# f.write(pickle.dumps(info))

f.close()

json反序列化2

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import pickle

def sayhi(name):
    print("hello2",name)

f = open("text.text","rb")
data = pickle.load(f)
# data = pickle.loads(f.read())
print(data["func"]("alex"))

json序列化3

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json

def sayhi(name):
    print("hello",name)

info = {
    'name':'alex',
    'age':22,
}
f = open("text.text","w")
f.write(json.dumps(info))

info['age'] = 21
f.write(json.dumps(info))
f.close()

json反序列化3

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json

f = open("text.text","r")
data = json.load(f)
print(data)

八、shelve模块

shelve模块是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import shelve
import datetime
d = shelve.open('shelve_test') # 打开一个文件
print(d.get("name"))
print(d.get("info"))
print(d.get("date"))
# info = {'age':22,"job":'it'}
# name = ["alex","rain","test"]
#
# d["name"] = name
# d["info"] = info
# d['date'] = datetime.datetime.now()
# d.close()

九、xml模块

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

xml的格式如下,就是通过<>节点来区别数据结构的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml 

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.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)

修改和删除xml文档内容

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter('year'):
    new_year = int(node.text) + 1
    node.text = str(new_year)
    node.set("updated","yes")
 
tree.write("xmltest.xml")
 
 
#删除node
for country in root.findall('country'):
   rank = int(country.find('rank').text)
   if rank > 50:
     root.remove(country)
 
tree.write('output.xml')

自己创建xml文档

import xml.etree.ElementTree as ET
 
 
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '33'
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'
 
et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
 
ET.dump(new_xml) #打印生成的格式

十、PyYAML模块

Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation 

十一、ConfigParser模块

用于生成和修改常见配置文档

来看一个好多软件的常见文档格式如下

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
 
[bitbucket.org]
User = hg
 
[topsecret.server.com]
Port = 50022
ForwardX11 = no

如果想用python生成一个这样的文档怎么做呢?

import configparser
 
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'}
 
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
   config.write(configfile)

写完了还可以再读出来哈。

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read('example.ini')
['example.ini']
>>> config.sections()
['bitbucket.org', 'topsecret.server.com']
>>> 'bitbucket.org' in config
True
>>> 'bytebong.com' in config
False
>>> config['bitbucket.org']['User']
'hg'
>>> config['DEFAULT']['Compression']
'yes'
>>> topsecret = config['topsecret.server.com']
>>> topsecret['ForwardX11']
'no'
>>> topsecret['Port']
'50022'
>>> for key in config['bitbucket.org']: print(key)
...
user
compressionlevel
serveraliveinterval
compression
forwardx11
>>> config['bitbucket.org']['ForwardX11']
'yes

configparser增删改查语

[section1]
k1 = v1
k2:v2
  
[section2]
k1 = v1
 
import ConfigParser
  
config = ConfigParser.ConfigParser()
config.read('i.cfg')
  
# ########## 读 ##########
#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模块

用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib

m = hashlib.md5()
m.update(b"Hello")
print(m.hexdigest())
m.update(b"It's me")
print(m.hexdigest())
m.update(b"It's been a long time since we spoken...")


m2 = hashlib.md5()
m2.update("HelloIt's me天王盖地虎".encode(encoding="utf-8"))
print(m2.hexdigest())

s2 = hashlib.sha1()
s2.update(b"HelloIt's me")
print(s2.hexdigest())

import hmac

h = hmac.new(b"12345","you are 250你是".encode(encoding="utf-8"))
print(h.digest())
print(h.hexdigest())

十三、re模块

常用正则表达式符号

'.'     默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
'^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
'$'     匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
'*'     匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']
'+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
'?'     匹配前一个字符1次或0次
'{m}'   匹配前一个字符m次
'{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
'|'     匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
'(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c
 
 
'\A'    只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的
'\Z'    匹配字符结尾,同$
'\d'    匹配数字0-9
'\D'    匹配非数字
'\w'    匹配[A-Za-z0-9]
'\W'    匹配非[A-Za-z0-9]
's'     匹配空白字符、\t、\n、\r , re.search("\s+","ab\tc1\n3").group() 结果 '\t'
 
'(?P<name>...)' 分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city") 结果{'province': '3714', 'city': '81', 'birthday': '1993'}

 

最常用的匹配语法

re.match 从头开始匹配
re.search 匹配包含
re.findall 把所有匹配到的字符放到以列表中的元素返回
re.splitall 以匹配到的字符当做列表分隔符
re.sub      匹配字符并替换

 

反斜杠的困扰
与大多数编程语言相同,正则表达式里使用"\"作为转义字符,这就可能造成反斜杠困扰。假如你需要匹配文本中的字符"\",那么使用编程语言表示的正则表达式里将需要4个反斜杠"\\\\":前两个和后两个分别用于在编程语言里转义成反斜杠,转换成两个反斜杠后再在正则表达式里转义成一个反斜杠。Python里的原生字符串很好地解决了这个问题,这个例子中的正则表达式可以使用r"\\"表示。同样,匹配一个数字的"\\d"可以写成r"\d"。有了原生字符串,你再也不用担心是不是漏写了反斜杠,写出来的表达式也更直观

仅需轻轻知道的几个匹配模式

re.I(re.IGNORECASE): 忽略大小写(括号内是完整写法,下同)
M(MULTILINE): 多行模式,改变'^''$'的行为(参见上图)
S(DOTALL): 点任意匹配模式,改变'.'的行为

 

作业

开发一个简单的python计算器

  1. 实现加减乘除及拓号优先级解析
  2. 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2) )等类似公式后,必须自己解析里面的(),+,-,*,/符号和公式(不能调用eval等类似功能偷懒实现),运算后得出结果,结果必须与真实的计算器所得出的结果一致

相关内容