my python day5

一、包和文件夹

包的本质就是一个文件夹,文件夹就是将文件组织起来

包和文件夹的区别就是多了__init__文件,导入包就会执行__init__.py。

二、模块

  一个模块就是包含一组功能的.py文件。如a.py 模块名就是a

模块分为三块:1.python标准库2.第三方库3.自定义模块  

导入文件

import + 模块名

from a import b  

模块导入过程中。1.执行被调用文件 2. 引入变量名 

如果被调用文件与执行文件具有相同的变量名或函数,执行文件就会覆盖调用文件

模块不会重复导入,只会在第一次import 的时候导入

脚本和模块:

脚本:一个文件就是整个程序,用来被执行

模块:文件中存放一堆功能,用来被导入

当一个文件当作脚本执行时,__name__ == "__main__"

当一个文件当作模块导入时,__name__就是模块名

解释器的搜索路径,放的是执行文件所在的路径,首先送内建模块中,然后在sys.path中找

绝对导入和相对导入

以执行文件的sys.path为起点开始导入,则为绝对导入

以当前文件夹为起点查找,则为相对导入

三、常用模块的使用

1.time模块

时间主要分为三类 1.时间戳(timestamp)2.结构化时间(struct time)3.字符串时间(format time)

时间戳:从1970年1月1日开始计时,是一个秒数

结构化时间:一个元组(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)

 1 import time
 2 print(time.time())  #时间戳
 3 print(time.gmtime(1515116))  #utc结构化时间   #把时间戳转为结构化时间
 4 print(time.localtime()) #当地结构化时间  把时间戳转为结构化时间
 5 print(time.strftime("%Y-%m-%d %H:%M:%S")) #字符串时间
 6 print(time.mktime(time.gmtime()))  #把结构化时间转为时间戳
 7 print(time.strftime("%Y-%m-%d %X",time.localtime()))  #把结构化时间转为字符串时间
 8 print(time.strptime("1990-05-02 12:15:32","%Y-%m-%d %X"))  #把字符串时间转为结构化时间
 9 print(time.ctime(time.time()))  #把时间戳转为固定格式的字符串时间
10 print(time.asctime(time.localtime()))  #把结构化时间转为字符串时间

2.datetime模块

1 import datetime,time
2 print(datetime.datetime.now())
3 print(datetime.date.fromtimestamp(time.time())) #把时间戳转为固定日期格式
4 print(datetime.datetime.now()+datetime.timedelta(days=3)) #当前日期+3天
5 print(datetime.datetime.now()+datetime.timedelta(hours=-3)) #当前日期-3小时
6 crime= datetime.datetime.now()
7 print(crime.replace(hour=3))  #替换时间

3.random模块

 1 import random
 2 print(random.random())  #0-1之间的浮点数
 3 print(random.randint(2,6))#[2-6]之间的随机整数
 4 print(random.randrange(2,6))#[2,6)之间的随机整数
 5 print(random.choice([2,6,3]))  #[2,6,3]中的随机数
 6 print(random.sample([2,6,3,5],2))  #[2,6,3,5]中随机选择2个数
 7 l= [1,2,3]
 8 random.shuffle(l)#对列表进行随机打乱
 9 print(l)
10 print(random.uniform(1,3))#大于1小于3的随机数
11 
12 #生成随机验证码
13 import random
14 def get_code(n):
15     codes=""
16     for i in range(n):
17         num = random.randint(0,9)
18         alp = chr(random.randint(65,122))
19         code  = random.choice([num,alp])
20         codes+=str(code)
21     return codes
22 codes = get_code(5)
23 print(codes)
View Code

4.os模块

 1 import os
 2 print(os.getcwd()) #当前目录
 3 os.chdir("dirname") #切换目录
 4 print(os.getcwd())
 5 print(os.curdir) #返回当前目录 (.)
 6 print(os.pardir) #返回父集目录(..)
 7 os.makedirs("./d1/d2")#生成多级目录
 8 os.removedirs("./d1/d2") #删除多级目录,只有目录为空时才能删除
 9 os.mkdir("./d1")#生成单级目录
10 os.rmdir("./d1")#目录为空时,删除单级目录
11 print(os.listdir("."))#显示目录下的所有文件
12 os.remove("./d")  #删除一个文件
13 os.rmdir("./d")
14 os.rename("./d1","d2")  #重命名目录或文件
15 print(os.stat("./test1.py"))#获取文件/目录信息
16 print(os.environ)#系统环境变量
17 print(os.path.abspath("./test1.py"))#绝对路径
18 print(os.path.split("./test1.py")) #对路径进行分割,得到一个元组
19 print(os.path.dirname("./test1.py")) #当前文件目录名
20 print(os.path.dirname("./test1.py"))#当前文件名
21 print(os.path.exists("./test1.py"))  #判断是否存在该文件
22 print(os.path.join(".","test2.py")) #文件路径拼接
23 print(os.path.join("d/d2","fd/g"))
24 print(os.path.isabs("./test1.py")) #判断是否绝对路径
25 print(os.path.isdir) #判断是否是一个目录
26 print(os.path.isfile("./test1.py"))  #判断是否是一个文件
27 print(os.path.getatime("./test2.py"))  #返回文件最后存取时间,是一个时间戳
28 print(os.path.getmtime("./test2.py")) #f返回文件最后修改时间
29 print(os.path.getsize("./test2.py")) #获取文件的大小
View Code

5.sys模块

1 import sys
2 print(sys.path)  #系统搜索路径
3 print(sys.version)   #python解释器的版本信息
4 print(sys.argv)  #命令行信息列表,第一个参数是程序本身路径

6.shutil模块

1 import shutil
2  shutil.copyfileobj(open("d.xml","r"),open("dnew.xml","w")) #将文件内容copy到另一个文件
3  shutil.copyfile("d.xml","d2.xml") #copy文件 ,新文件无需存在

7.zipfile模块

import zipfile
#压缩文件
z = zipfile.ZipFile("new","w")
z.write("dd")
z.close()
#解压文件
z = zipfile.ZipFile("new","r")
z.extractall("new2")
z.close()

 8.json&pick模块

序列化:把一个变量从内存中转为可存储或可传输的过程。dumpdumps

反序列化:把可存储和可传输的数据重新读取到内存中. loadloads

在json格式中,字符串单引改双引

 1 import json
 2 data ={"name":"Ezhizen","age":18}
 3 with open("info.txt","w") as f:
 4     data = json.dump(data)   #序列化
 5 #     f.write(data)
 6 with open("info2.txt","w") as f:
 7     json.dump(data,f)  #序列化
 8 
 9 
10 with open("info.txt","r") as f:
11     data = json.loads(f.read())  #反序列化
12     print(data)
13 with open("info2.txt","r") as f:
14     data = json.load(f)    #反序列化
15     print(data)
16 data ={"name":"Ezhizen","age":18}
17 import pickle
18 with open("info3.txt","wb") as f:
19     data = pickle.dumps(data)    #序列化
20     f.write(data)
21 with open("info3.txt","wb") as f:
22     pickle.dump(data,f)  #序列化
23 with open("info3.txt","rb") as f:
24     data = pickle.loads(f.read())  #反序列化
25     print(data)
26 with open("info3.txt", "rb") as f:
27     data = pickle.load(f)  #反序列化
28    print(data
View Code

9.shelve模块

import shelve

f=shelve.open(r'sheve.txt')
# f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}
# f['stu2_info']={'name':'gangdan','age':53}
# f['school_info']={'website':'http://www.pypy.org','city':'beijing'}

print(f['stu1_info']['hobby'])
f.close()
View Code

10.xml模块

 1 #自己生成文档树
 2 import xml.etree.ElementTree as ET
 3 new_xml = ET.Element("Info")
 4 name = ET.SubElement(new_xml,"name",attrib={"Ezhizen":"Yes"})
 5 age = ET.SubElement(name,"age")
 6 age.text= "18"
 7 name = ET.SubElement(new_xml,"name",attrib={"Echizen":"NO"})
 8 age = ET.SubElement(name,"age")
 9 age.text ="16"
10 et= ET.ElementTree(new_xml)  #生成文档树
11 et.write("info.xml",encoding="utf-8",xml_declaration=True)
View Code
 1 import xml.etree.ElementTree as ET
 2 tree = ET.parse("old.xml")  #得到树对象
 3 root = tree.getroot()
 4 for country in root:
 5     print(country) #得到所有的子对象
 6     print(country.tag)  #得到所有的子对象标签
 7     for i in country:
 8         print(i) #得到所有的孙对象
 9         print(i.tag,i.attrib,i.text) #得到孙对象的标签、属性、文本
10 #修改和添加
11 for year in root.iter("year"):
12     new_year = int(year.text)+1
13     year.text = new_year 
14     year.attrib={"updated","yes"}
15     year.set("d","f")
16 tree.write("文件名") 
17 #删除
18 for country in root.findall("country"):
19     rank = country.find("rank")
20     if int(rank)<5:
21         root.remove(country)
22 tree.write("文件名") 
View Code

11.configparse 模块

配置文件

 1 import configparser,os
 2 config = configparser.ConfigParser() #获得config对象
 3 config.read("info3.txt") #从文件中读取
 4 sections = config.sections()  #获得所有的标题
 5 # print(sections)
 6 options= config.options("section1") #获得section1下所有的键
 7 print(options)
 8 items = config.items("section1") #获得section1下所有的键值对
 9 print(items)
10 value = config.get("section1","k1") #获得section1下看k1对应的value 字符串格式
11 print(value)
12 value2 = config.getint("section1","age")  #int格式
13 print(type(value2))
14 config.remove_section("section2")  #删除section2标题
15 config.remove_option("section2","k1")  #删除section2中的k1
16 print(config.options("section2"))
17 config.add_section("new")  #添加一个标题
18 config.set("new","name","Ezhizen") #在new标题下添加内容
19 path2 = os.path.abspath(__file__)
20 config.set("new","path",path2)  #添加路径
21 config.write(open("new.txt","w"))
View Code

12.hashlib模块

hash是一种算法,传入内容经过hash处理得到一连串的hash值

1.只要传入的内容一样,hash处理后的值一定一样

2.不能由hash值反解成内容

3.只要hash算法不变,得到hash值的长度是一样的

import hashlib
m = hashlib.md5()
m.update("hello".encode("utf-8"))
print(m.hexdigest())

13logging模块

import logging
logging.basicConfig(level="DEBUG",
                    filename="log",
                    filemode="w",
                    format="%(asctime)s%(message)s")
logging.debug("debug message")
logging.info("info message")
logging.warning("warning message")
logging.error("error message")
logging.critical("critical message")

import logging
def get_logger():
    logger = logging.Logger("root")
    fh = logging.FileHandler("log.txt",mode="w")
    sh = logging.StreamHandler()
    fm = logging.Formatter("%(asctime)s%(message)s")
    fh.setFormatter(fm)
    sh.setFormatter(fm)
    logger.addHandler(fh)
    logger.addHandler(sh)
    logger.setLevel(1)
    logger.debug("debug message")
    logger.info("info message")
    logger.warning("warning message")
    logger.error("error message")
    logger.critical("critical message")
get_logger()
View Code

14re模块

re模块的作用是进行模糊匹配

.匹配任意字符

^以什么开头

$以什么结尾

*(0,)

| 或

[] 匹配多个字符

[-] 表示范围[^]非转义

d匹配任意数字

D匹配任意非数字

s匹配任意空白字符

S匹配任意非空白字符

w匹配任意字母数字

W匹配任意非字母数字

()分组

 1 import re
 2 print(re.findall("a.b","alb")) #['alb'] 任意字符
 3 print(re.findall("^ab","abc"))  #['ab']  以什么开头
 4 print(re.findall("ab$","cab")) #['ab'] 以什么结尾
 5 print(re.findall("ab*","abbbbb"))  #['abbbbb'] (0,)
 6 print(re.findall("ab+","abbbb"))  #['abbbb'] (1,)
 7 print(re.findall("ab?","a")) #['a'] (0,1)
 8 print(re.findall("ab{2}","abbbb"))  #['abb']2次
 9 print(re.findall("ab{0,1}","abb"))   #['ab'] (1,3)次==?
10 print(re.findall("ab{1,3}","abb")) #['abb']  (1,3)==+
11 # {0,} == *
12 print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b'] 贪婪匹配
13 print(re.findall('a.*?b','a1b22222222b')) #['a1b']非贪婪匹配
14 print(re.findall('a[1*-]b','a1b a*b a-b'))  #匹配多个字符 ['a1b', 'a*b', 'a-b']
15 print(re.findall('a[^1*-]b','a1b a*b a-b a=b'))  #^非['a=b']
16 print(re.findall('w+','hello egon 123'))  #w所有的字母数字['hello', 'egon', '123']
17 print(re.findall('W','hello egon 123')) #[' ', ' ']#W 非字母数字
18 print(re.findall('s+','hello  egon  123'))#[' ', ' ']空白字符
19 print(re.findall('S+','hello  egon  123')) #['hello', 'egon', '123']非空白字符
20 print(re.findall('d+','hello egon 123')) #['123']  数字
21 print(re.findall('D+','hello egon 123')) #['hello egon '] 非数字
22 print(re.findall('a\\c','ac'))  #['a\c']使用原生字符串且不转义
23 print(re.findall(r'a\c','ac')) #['a\c']
24 print(re.findall("ab|c","abccc"))  #['ab', 'c', 'c', 'c']或
25 print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab']
26 print(re.findall('(ab)+123','ababab123'))#['ab']匹配到末尾的ab123中的ab但只显示分组内容
27 print(re.findall('(?:ab)+123','ababab123')) #['ababab123']显示匹配到的所有内容
28 print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">点击</a>'))
29 #['http://www.baidu.com'] 显示分组内容
30 print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">点击</a>'))
31 #['href="http://www.baidu.com"'],显示所有内容
32 # print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))
33 print(re.findall('e','alex make love') )#返回所有满足匹配条件的结果,放在列表里
34 print(re.search('e','alex make love').group())
35 #只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。
36 print(re.match('e','alex make love')) #只在开头进行匹配None
37 print(re.split('[ab]','abcd')) #['', '', 'cd'] 先按'a'分割得到''和'bcd',再对''和'bcd'分别按'b'分割
38 print('===>',re.sub('a','A','alex make love')) ##===> Alex mAke love,不指定n,默认替换所有
39 print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love 替换第一个
40 # print('===>',re.sub('^(w+)(.*?s)(w+)(.*?s)(w+)(.*?)$',r'52341','alex make love'))
41 print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love 替换2个
42 obj=re.compile('d{2}')
43 print(obj.search('abc123eeee').group()) #12
44 print(obj.findall('abc123eeee')) #['12'],重用了obj
View Code

 15、copy模块

 1 import copy
 2 l = [1,2,[3,4]]
 3 l2 = copy.copy(l) #浅copy
 4 l2[2][0] = 5
 5 print(l)  #浅copy,只能copy第一层,改变第二层的值,就会改变原copy对象的值
 6 print(l2)   
 7 
 8 l3 = copy.deepcopy(l)
 9 l3[2][0] = 6
10 print(l2)
11 print(l) #深copy,改变第二层,也不会改变原copy对象的值
View Code
原文地址:https://www.cnblogs.com/Ezhizen/p/11181119.html