四,常用模块

1.

模块:本质就是以.py结尾 的python文件

包:用来从逻辑上组织模块的,本质就是一个目录(必须带有__init__.py文件),导入包,就是执行该包下__init__.py文件中的代码

1.1 Json&pickle 数据序列化,Json 用于交互,只能用于简单转换(字符串或者字典等),xml也可以用于交互、

info = {"name":"Alex" ,
        "age": 22}
f = open("file.text","w")#把对象转换成字符串
f.write(str(info))#存进去
f.close()

f_file = open("file.text","r")
f_read = eval(f_file.read())#读出来
print(f_read)
print(f_read["name"])
View Code
import json
info = {"name":"Alex" ,
"age": 22}
f = open("file.text","w")
json.dump(info,f)#存进去
f.close()

f_file = open("file.text","r")
f_read = json.loads(f_file.read())#读出来
print(f_read)
print(f_read["name"])

1.2 pickle 可以序列化所有内容,只能在python中使用,且转换成二进制,以“wb”打开

2 文件规范和绝对路经

简要介绍文件:

  1.bin/:存放项目的一些可执行文件,当然你可以起名acript/之类的

  2.foo/:存放项目的源代码:

    (1)源代码中的所有模块,包都应该放在此层目录

    (2)其子目录tests/存放单元测试代码

    (3)程序的入口最好命名为main.py

  3.docs/:存放一些文档

  4.setup.py:安装,部署,打包的脚本

  5.requirements.txt:存放软件以来的外部Python宝列表

  6.README:项目的说明文件 

import os
import sys

# 如何找到父辈下的子代

# 如何获取当前文件的路径


print('-----相对路径-----')
# 相对路径:终端下执行命令,进入文件 python atm.py
print(__file__)

print('-----绝对路径:需要导入os库-----')
# 如果想要当前文件的绝对路径的话,导入库os
print(os.path.abspath(__file__))

print('-----父亲级目录-----')
# 当前文件上一级路径
print(os.path.dirname(os.path.abspath(__file__)))

print('-----爷爷级目录-----')
# 爷爷级目录
print(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# 爷爷级目录路径
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

sys.path.append(BASE_DIR)

# import configure,core
# 从叔叔级目录下导入需要的文件名
from configure import setting
from core import main

# 文件调取函数
main.login()
View Code

3.xml(是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用更简单)

from xml.etree import ElementTree as ET
def build_sitemap():
    urlset = ET.Element("urlset")   #设置一个根节点,标签为urlset
    url = ET.SubElement(urlset,"url")   #在根节点urlset下建立子节点
    loc = ET.SubElement(url,"loc")
    loc.text = "http://www/baidu.com"
    lastmod = ET.SubElement(url,"lastmod")
    lastmod.text = "2017-10-10"
    changefreq = ET.SubElement(url,"changefreq")
    changefreq.text = "daily"
    priority = ET.SubElement(url,"priority")
    priority.text = "1.0"
    tree = ET.ElementTree(urlset)
    tree.write("sitemap.xml")
if __name__ == '__main__':
    build_sitemap()
View Code

 4.time, datetime

import time
'''
t = time.time()#时间戳是为了unix诞生的时间
print(x)
#>>1575938414.7207327#单位s
print(1970 + (t/3600/24/365))
#>>2019.9726834060823#单位年
time.gmtime()#utc-o时区的时间的元组形式,即标准时区的时间,与中国差八小时,也可以传入时间戳参数,转换成标准时间元组
time.gmtime(1255555555)
print(time.localtime())#本地元组形式时间,tm_isdst是时区 0代表不是夏令时,也可以传入时间戳参数,转换成本地时间元组
#>>time.struct_time(tm_year=2019, tm_mon=12, tm_mday=10, tm_hour=8, tm_min=48, tm_sec=28, tm_wday=1, tm_yday=344, tm_isdst=0)
x = time.localtime()
print(x.tm_year)#取出元组中的相关元素
#>>2019
print(time.mktime(x))#把元组形式转换成时间戳
#>>1575939604.0
#中国处于东八区,比标准时间早8小时,
t_zone = time.timezone
print(t_zone/3600)
#>>-8.0
print(time.altzone)#夏令时与UTC的时间差
#>>-32400
print(time.daylight)#是否使用了夏令时
#>>0
time.sleep(1)#停顿时间
'''
t = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())#把元组转换成指定模式时间,前面放格式,后面放元组
print(t)
#>>2019-12-10 09:00:04
print(time.strptime("2006-2-5","%Y-%m-%d"))#前面放字符串,后面放格式,必须一一对应,最终转换成元组
View Code
 
import time datetime
print(time.ctime())#把时间戳转换成%a %b %d %H %M %S %Y 的形式
#>>Tue Dec 10 09:44:43 2019
print(time.asctime())#把元组转换成%a %b %d %H %M %S %Y 的形式
#>>Tue Dec 10 09:44:43 2019

import datetime#犯了一个低级错误,把文件名写成了datetime,导致一直报错
now = datetime.datetime.now()#获取现在时间
print(now)
#>>2019-12-10 10:03:06.830886
now = datetime.datetime.now() + datetime.timedelta(hours=3)#当前时间+ 指定时间
print(now)
View Code

 5.random 随机模块

import random
'''
#r_now = random.random()#随机[0,1)之间的浮点数
a = 0
while a < 10 :
#    r_now = random.randint(0,9) #随机[a,b]之间的整数
    r_now = random.randrange(0,9) #随机[a,b)之间的整数
    print(r_now)
    a += 1
'''

#r = random.choice("123456")#()中可以是字符串,列表,元组
r = random.choice([1,2,3])
print(r)
r_d = random.sample([1,2,3],2)#前面与choice 一样是个序列,后面跟取几位
print(r_d)

a = {"name":"alex",
     "age":"23"}
print(list(a))
r_d = random.sample(list(a),2)#如果先随机字典,要先转换成列表,且只能随机 keys
print(r_d)
#>>['age', 'name']
x = random.uniform(1,2)#取(a,b)之间的浮点数
print(x)
a = [1,2,3]
random.shuffle(a)#打乱a中的数列顺序
print(a)
View Cod
"写一个由4位组成的随机数字+字母的组合,可以当验证码"
checkcode = ''
for i in range(0,4):#3位中随机一个
    current = random.randrange(0,4)#3位中随机一个
    if current == i :
        tmp = chr(random.randint(65,90))#chr()把数字转换成字母,[65,90]
    else :
        tmp = random.randint(0,9)
    checkcode += str(tmp)
print(checkcode)
View Code

6.os模块

import os
1.1
print(os.getcwd())#获取当前的工作目录
#>>D:\python_work\19-12\10_file
1.2
os.chdir(r"D:\python_work\19-12")#与getcwd 相互使用,转到指定目录,
print(os.getcwd())#获取当前的工作目录
#>>D:\python_work\19-12
1.3
print(os.curdir)#返回当前目录,不是方法是一个属性
#>> .#.代表当前目录
1.4
print(os.pardir)#返回父级目录
#>>..
1.5
os.makedirs("D:\python_work\19-12\10_file")#创建或递归创建目录,既使不存在目录,也可以一下创建多级目录
1.6
os.removedirs(r"D:\python_work\19-12\11_file")#从最低开始删除,直到文件夹被空,空文件删除
1.7
os.mkdir(r"D:\python_work\19-12\11_file")#生成单级目录,不可多级创建
1.8
os.rmdir(r"D:\python_work\19-12\11_file")#删除单级目录,只删空文件夹
1.9
print(os.listdir(r"."))#返回当前目录文件夹中的文件
1.10
os.rename("file-random.py","file_random.py")#改名,只能改当前文件夹中的
1.11
print(os.stat("file_os.py"))#获取文件或目录信息
View Code
1.1
print(os.sep)#返回操作系统特定的路径分割符,win为"\\",linux 为"/"
1.2
print(os.linesep)#输出当前平台使用的行终止符,win为"\r\t",linux 为"\n"
1.3
print(os.environ)#返回环境变量
1.4
print(os.pathsep)#输出用于分割文件名的分隔符,win下为";"
1.5
print(os.name)#输出平台名,wen下为“nt”, linux为"posix"
1.6
print(os.system("dir"))#执行命令,dir为当前目录下的文件
1.7
print(os.system("ipconfig/all"))#ipconfig/all 为IP配置等
1.8
print(__file__)#相对路径
print(os.path.abspath(__file__))#绝对路径
1.9
print(os.path.split(r"D:\python_work\19-12\10_file\file_os.py"))#分割,并显示出来
1.10
print(os.path.basename(r"D:\python_work\19-12\10_file\file.py"))#把最后一个分割出来,可以不存在
1.11
print(os.path.exists(r"D:\python_work\19-12\10_file"))#判断目录是否存在,存在返回True,反之false
1.12
print(os.path.isabs(r"D:\python_work\19-12\10_file"))#如果是绝对路径,则返回True,绝对路径就是带根目录的,win下为 c,d,e,f盘,linux下为"/"
1.13
print(os.path.isfile(r"D:\python_work\19-12\10_file\file_os.py"))#判断是否是文件
1.14
print(os.path.isdir(r"D:\python_work\19-12\10_file"))#判断是否是目录
1.15
print(os.path.getatime(r"D:\python_work\19-12\10_file"))#返回所指向文件或者文件夹的最后存取时间,返回的是时间戳
1.16
print(os.path.getmtime(r"D:\python_work\19-12\10_file"))#返回最后修改时间,返回的是时间戳
1.17
print(os.path.join(r"c:",r"\a.txt"))#将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
View Code

 7.sys模块

import sys
#1.1
print(sys.argv)#  sys.argv[]说白了就是一个从程序外部获取参数的桥梁,这个“外部”很关键,
# 所以那些试图从代码来说明它作用的解释一直没看明白。因为我们从外部取得的参数可以是多个,
# 所以获得的是一个列表(list),也就是说sys.argv其实可以看作是一个列表,
# 所以才能用[]提取其中的元素。其第一个元素是程序本身,随后才依次是外部给予的参数
#1.2
sys.exit()#退出程序,正常退出时用exit(0)
#1.3
print(sys.version)#获取python解释程序的版本信息
#1.4
print(sys.maxsize)#最大值
#1.5
print(sys.path.append())#把目录添加到当前目录
#1.6
print(sys.platform)#返回操作系统平台名称
View Code

8.shutill 模块(高级的文件、文件夹、压缩包处理模块)

   shutill 对压缩包的处理是调用zipfile和TarFile两个模块来进行的

import shutil

#1.1
f_one = open("file_one","r",encoding="utf-8")
f_two =  open("file_two","w",encoding="utf-8")
shutil.copyfileobj(f_one,f_two)#把f_one中的内容copy到file_two中
#1.2
shutil.copyfile(file_one,file_two)#把file_one中的内容copy到file_two中,是copyfileobj的简化版
#1.3
shutil.copymode("file_one","file_two")#只拷贝权限,内容、组、用户均不变
#1.4
shutil.copystat("file_one","file_two")#拷贝状态的信息,包括:mode bits ,qtime,mtime,flags
#1.5
shutil.copytree("a","123")#递归的拷贝文件夹,即拷贝多层目录
#1.6
shutil.rmtree("a")#递归的删除文件夹,即删除多层目录
#1.7
print(shutil.make_archive("file_ttt","zip"))#(base_name,format.......),base_name:压缩包的文件名,也可以是压缩包的路径。只是文件名是,保存至当前目录,否则保存到指定路径
#format:压缩包种类,"zip","tar","bztar","gztar" ; root_dir:要压缩的文件夹目录(默认当前目录);owner:用户,默认当前用户
#group:组,默认当前组 ;  logger: 用于记录日志,通常是logging.logger 对象

shutil.make_archive("file——ttt","zip",r"D:\python_work\11")#把D:\python_work\11下的文件夹以"Zip"打包到当前目录下file——ttt中
View Code

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

import shelve,datetime
d = shelve.open("tttt")#新创一个文件
info = {"age":23,"name":"alex"}
name = ["alex","rain","blue"]
d["name"] = name  #持久化列表
d["info"] = info
d["data"] = datetime.datetime.now()
print(d.get("name"))#根据k值调value
print(d.get("data"))
print(d.get("info"))
d.close()
View Code

10 configparser模块(用于生成和修改常见配置文件)

import configparser
config = configparser.ConfigParser()#生成与ha.conf一样的配置文件
config["DEFAULT"] = {"ServerAliveInterval":"45",
                     "Comperssion":"yes",
                     "CompressionLevel":"9"}
config["bitbucket.org"] = {}
config["bitbucket.org"]["user"]="hg"
config["topsecret.server.com"]={}
topsecret = config["topsecret.server.com"]
topsecret["Host Port"] = "50022"
topsecret["ForwardXll"] = "no"
config["DEFAULT"]["Forwardxll"] = "yes"

with open("example.ini","w") as configfile:
    config.write(configfile)
"""
[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardXll = yes

[bitbucket.org]
User = hg

[topsecret.server.com]
Port = 50022
ForwardXll = no
"""
View Code
conf = configparser.ConfigParser()#读取配置文件
conf.read("example.ini")
print(conf.defaults())
print(conf["bitbucket.org"]["user"])
View Code

11.hashlib模块

     用于加密相关操作,3.x里代替了MD5和shamok,主要提供SHA1,SHA256,SHA384,SHA512,MD5算法,字典就是hashlib做的,映射关系

import hashlib
m = hashlib.md5()#生成加密对象,加密类型是什么就跟什么
m.update(b"hello")#b=bytes类型,放入内容
m.update(b"It's me")#中文,"你好".encoding(encoding="utf-8")
m.update("你好".encode(encoding="utf-8"))#中文,"你好".encoding(encoding="utf-8")
print(m.digest())#以二进制格式hash
#>>b'C+\x15Id\tq<=\x03l\x06x\xc2CU'
print(len(m.digest()))
#>>16
print(len(m.hexdigest()))#以十六进制进行hash
#>>32
print(m.hexdigest())
#>>432b15496409713c3d036c0678c24355
View Code

12.hmac模块(它内部对我们创建key和value再进行加密)

import hmac
h = hmac.new(b"alex",b"23")#"中文".encode(encoding="utf-8")
print(h.hexdigest())#十六进制
print(h.digest())#二进制
View Code

 13.re模块

match()是从头开始匹配,search()从左往右搜索

import re
#1.1 "."
print(re.search(".c","abcd"))#"."匹配任意一个字符,"\n"除外,可前可后,只有"."从头开始匹配
#1.2 "字符串"
print(re.search("bc","abcd"))#匹配任意指定字符
#1.3 [0-9] ,[012]
str_1 ="12abcd89apple@dragon$33"
print(re.search("[0-9]",str_1))#匹配来自字符串中的范围内的一个字符,[1234]匹配任意一个字符
#>><re.Match object; span=(0, 1), match='1'>
print(re.search("[082]",str_1))#匹配任意一个字符,先匹配字符串中先出现的,不是符号中先出现的
#>><re.Match object; span=(0, 1), match='2'>
1.4 ^
print(re.search("^12D",str_1))#脱字符匹配字符串起始部分,即匹配起始是12D的字符
1.5 $ 美元符
print(re.search("33$",str_1))#美元字符匹配字符串结尾部分,即匹配结尾是33的字符
1.6  +
print(re.search("cd[6-9]+",str_1))#"+"匹配前面出现的正则表达式1次或多次,即最低一次
1.7print(re.search("[1]?",str_1))#"?"匹配前面出现的正则表达式0次或1次,即最多一次
1.8 {n}
print(re.search("[1-9]{4}",str_1))#"+"匹配{n}n次前面的正则,不是一次一次,而是叠加,即4次则6789要在一起
1.9 re.findall()
print(re.findall("[1-9]{2}",str_1))#显示所有两位的组合
#>>['12', '67', '89', '89', '33']
 1.10{m,n}
print(re.findall("[1-9]{1,4}",str_1))#有两位显示两位,优先显示多为
#>>['12', '6789', '8933']
1.11 
print(re.search("[1-9]{1,5}",str_1))#不是由{1,5}来定几位。而是由字符本身含有的来定,从头开始,有一位显示一位,有两位优先显示两位
View Code
import re
str_1 ="12Dabcd6789apple@dragon8933"
1.1 | 管道符号
print(re.search("12Da|abc",str_1).group())#返回字符串类型
#>>12Da
print(re.search("12Da|abc",str_1))
#>><re.Match object; span=(0, 4), match='12Da'>

print(re.findall("12Da|abc",str_1))#返回列表类型
#>>['12Da']

1.2 
m = re.match("3.14","3.14")#“.”可以匹配除\n之外的任何字符,包括它自己
if m is not None:
    print(m.group())

#1.3 ()用括号进行匹配和保存分组
m = re.match("(\w\w\w)-(\d\d\\d)","aaa-234")
if m is not None:
    print(m.group())#(1)可以选择显示子组
#>>aaa-234
    print(m.groups())#显示所有子组
#>>('aaa', '234')

#1.4 /b /B ,单词边界和没有边界
m = re.search(r"\bThe","The bite blue")#在边界
if m is not None:
    print(m.group())
#>>The
m = re.search(r"\bThe","bite The blue")#在边界
if m is not None:
    print(m.group())
#>>The
m = re.search(r"\bThe","biteThe blue")#不在边界
if m is not None:
    print(m.group())
#>>
m = re.search(r"\BThe","biteThe blue")#\B没有边界,有边界则None
if m is not None:
    print(m.group())
#>>The

#1.5  (?P<命名>),以字典方式存储,可动态分组
a = re.search("(?P<id>\d+)(?P<name>[a-zA-Z]+)","adc123qwe123")
print(a.group())
#>>123qwe
print(a.groupdict())
#>>{'id': '123', 'name': 'qwe'}
a = re.search("(?P<year>[0-9]{4})(?P<month>[0-9]{2})(?P<day>[0-9]{2})","20500101")#分组
#>>{'year': '2050', 'month': '01', 'day': '01'}

#1.6 split() 分割字符串
print(re.split("[0-9]+","ab12cd3ef"))#返回一个列表
#>>['ab', 'cd', 'ef']

f = open("whodata","r")#
for eachline in f:
    print(re.split(r"\s\s+",eachline))#以空格分割文件每一行
f.close()
 
#1.7  sub() 替换 ,subn()返回替换了几次
print(re.sub("[0-9]","|","ab12cd3ef45",count=2))#[0-9]{2},表示只替换两位的数字,替换两次
#>>ab||cd3ef45

print(re.sub("[ab]","|","ab12cd3ef45")#[ab],ab都替换
#>>||12cd3ef45

#1.8 (?i)#对指定一个或者多个标记,不区分大小写
a = re.findall("(?i)yes","yes? Yes,YES y!!!")
print(a)
#>>['yes', 'Yes', 'YES']
View Code

猜你喜欢

转载自www.cnblogs.com/huiguizhe/p/12009461.html