WEEK5:常用模块学习

  • 定义
    • 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件
    • 包:用来从逻辑上组织组织模块的,本质就是一个目录(必须带有一个__init__.py文件)
  • 导入方法
    • 导入模块
      • import module_name,module2_name  #导入模块
      • from module_alex import *  #导入module_alex模块中所有的方法,不建议使用此方法
      • from module_alex import logger as logger_alex  #导入模块中的特定方法logger并将其重命名logger_alex
      • from module_alex import logger1,logger2  #导入多个方法
    • 导入包
      • import package_test #packet_test为文件夹名字,内部还有很多模块和一个__init__.py文件
  • import的本质(路径搜索和搜索路径)
    • 导入模块的本质就是把python文件解释一遍(将模块赋给一个变量,变量的名字就是模块的名字)
    • 导入包的本质就是执行该包下的__init__.py文件
      __init__.py文件中必须将该文件夹下面的模块全部“from . import 模块名字”一遍,否则之后无法调用
  • 导入优化
    • 将自己的模块和包的路径添加到sys.path这个列表的最前面
      x=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
      sys.path.insert(x) #插入最前面
      sys.path.append(x) #插入最后面
  • 模块分类
    • 标准库(内置模块)
      • 时间模块time与datetime
        • time
          import time
          • 时间方式  
            • 时间戳 time.time()
            • 格式化的时间字符串
            • 元组(struct_time,共九个元素:年,月,日,时,分,秒,一周的第几天,一年的第几天,是否是夏令时)time.localtime()
          • 方法
            • timezone() #当前时区时间与utc相差多少秒
            • gmtime() #将时间戳转换成标准时间(格林尼治时间)元组struct_time
            • localtime() #将时间戳转换成本地时间元组struct_time
              与其相反的函数是mktime()
            • strftime(格式,struct_time) #将元组时间转换成格式化的时间字符串,%Y年%m月%d天%H小时等
              与其相反的函数是strptime(字符串,格式)
            • asctime() #将元组时间转换成“Sat Aug 14:59:45 2016”格式
            • ctime() #将时间戳转换成“Sat Aug 14:59:45 2016”格式
            • sleep() #休眠几秒
        • datetime
          import datetime
          • datatime.datatime.now()  #获取当前时间
          • datetime.datatime.now()+datetime.timedelta(3) #3天后的时间
          • datetime.datatime.now()+datetime.timedelta(3) #3天前的时间
          • datetime.datatime.now()+datetime.timedelta(hours=3) #3个小时后的时间
          • datetime.datatime.now()+datetime.timedelta(hours=-3) #3个小时前的时间 #分钟是minutes
      • random #随机数
        • random.random() #随机数0-1之间
        • random.randint(1,3) #随机1~2之间的整数
        • random.choice("序列")  #从序列中获取随意值
        • random.sample("序列",长度) #从序列中随机取出指定长度的元素
        • random.uniform(1,10) #取1-10之间的随意浮点数
        • random.shuffle() #将一个有序的序列重新排列
        • 生成验证码
           1 import random
           2 checkcode=''
           3 for i in range(4):
           4     current=random.randrange(0,4)
           5     if current==i:
           6         tmp=chr(random.randint(65,90))
           7     else:
           8         tmp=random.randint(0,9)
           9     checkcode+=str(tmp)
          10 print(checkcode)
          View Code
      • os #提供对操作系统进行调用的接口
        • os.getcwd() #获取当前的操作目录
        • os.chdir("C:\\Users") #改变当前脚本工作目录
        • os.curdir() #返回当前目录
        • os.pardir() #返回当前目录的父目录
        • os.makedirs() #递归的创建目录
        • os.removedirs() #递归的删除目录
        • os.mkdir() #非递归创建目录
        • os.rmdir() #删除单级空目录
        • os.listdir() #列出当前目录的内容列表
        • os.remove() #删除一个文件
        • os.rename() #重命名文件或者目录
        • os.stat() #获取文件目录的信息
        • os.sep #输出操作系统特定的路径分隔符,win下为“\\”,linux下为“/”
        • os.linesep #输出操作系统特定的路径分隔符,win下为“\t\n”,linux下为“\n”
        • os.pathsep #输出用于分割文件路径的字符串
        • os.name 输出字符串指示当前使用平台,nt->win,posix->linux
        • os.system("cmd命令")  #运行cmd命令
        • 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最后的文件名,其实就是os.path.split(path)的第二个元素
        • os.path.exists(path) #判断path是否存在
        • os.path.isabs(path) #判断path是否为绝对路径
        • os.path.isfile(path) #判断是否为文件
        • os.path.join(path1,path2,...) #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
        • os.path.getatime() #返回最后存取时间
        • os.path.getatime() #返回最后修改时间
      • sys
        • sys.argv #命令行参数list,第一个元素是程序本身路径
        • sys.exit(n) #退出程序,正常退出时exit(0)
        • sys.version #获取python解释程序的版本信息
        • sys.maxint #最大的Int值
        • sys.path #返回模块的搜索路径,
        • sys.platform #返回操作系统平台名称
      • shutil #高级的文件、文件夹、压缩包处理模块
        • shutil.copyfileobj() #将文件内容拷贝到另一个文件,可以是一部分内容
        • shutil.copyfile() #拷贝文件
        • shutil.copymode() #仅拷贝权限,内容、组、用户均不变
        • shutil.copystat() #拷贝状态信息,包括mode,utime,mtime,atime等信息
        • shutil.copy() #拷贝文件和权限
        • shutil.copy2() #拷贝文件和状态信息
        • shutil.copytree() #递归的拷贝文件
        • shutil.rmtree() #递归的删除目录
        • shutil.move() #递归的去移动文件
        • shutil.make_archive(压缩包的文件名,压缩包种类,被压缩的文件或目录) #创建压缩包并返回文件路径
      • shelve #是一个简单的k,v将内存数据通过文件持久化的模块,可以持久化任何pickle可支持的python数据格式
      • xml处理模块
        import xml.etree.ElementTree as ET
        tree=ET.parse("xmltest.xml")
        root=tree.getroot()
        print(root) #内存地址
        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)

        #修改,把每个year的值+1
        for node in root.iter("year"):
            new_year=int(node.text)+1
            node.text=str(new_year)
            node.set("updated_by","Alex")
        tree.write("xmltest.xml") #写回源文件

        #删除node,删除排名50以后的
        for country in root.findall("country"):
            rank=int(country.find('rank').text)
            if rank > 50:
                root.remove(country)
        tree.write("output.xml") #写入另外一个文件

        #创建xml文档
        new_xml = ET.Element("personinfolist")
        personinfo = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "yes"})
        name = ET.SubElement(personinfo, "name")
        name.text = "Alex Li"
        age = ET.SubElement(personinfo, "age", attrib={"checked": "no"})
        sex = ET.SubElement(personinfo, "sex")
        age.text = '56'
        personinfo2 = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "no"})
        name = ET.SubElement(personinfo2, "name")
        name.text = "Oldboy Ran"
        age = ET.SubElement(personinfo2, "age")
        age.text = '19'
        et = ET.ElementTree(new_xml)  # 生成文档对象
        et.write("test.xml", encoding="utf-8", xml_declaration=True)
        ET.dump(new_xml)  # 打印生成的格式
      • configparser模块 #用于生成和修改常见配置文档
        • 配置文件如下
          [DEFAULT]
          ServerAliveInterval =45
          Compression =yes
          CompressionLevel =9
          ForwardX11 =yes
          [bitbucket.org]
          User =hg
          [topsecret.server.com]
          Port =50022
          ForwardX11 =no
        • 生成配置文件
          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
          conf=configparser.ConfigParser()
          conf.read("example.ini")
          print(conf.defaults())
          print(conf['bitbucket.org']['user'])
        • 删除
          sec=conf.remove_section('bitbucket.org')
          conf.write(open("example.ini",'w'))
      • hashlib模块 #用于加密相关的操作,主要提供SHA1,SHA224,SHA256,SHA384,SHA512,MD5算法
        importhashlib
        • 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())
        • python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密。散列消息鉴别码,简称HMAC,是一种基于消息鉴别码MAC(Message Authentication Code)的鉴别机制。使用HMAC时,消息通讯的双方,通过验证消息中加入的鉴别密钥K来鉴别消息的真伪;一般用于网络通信中消息加密,前提是双方先要约定好key,就像接头暗号一样,然后消息发送把用key把消息加密,接收方用key + 消息明文再加密,拿加密后的值 跟 发送者的相对比是否相等,这样就能验证消息的真实性,及发送者的合法性了。
          import hmac
          h = hmac.new(b'12345', 'you are 250你是'.encode(encoding="utf-8"))
          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 #匹配字符并替换
          • 匹配模式:
            flags=匹配模式
            • re.I(re.IGNORECASE) #忽略大小写(括号内是完整写法)
            • re.M(re.MULTILINE) #多行模式,改变^和$的行为
            • re.S(re.DOTALL) #点任意匹配模式,改变.的行为
                
    • 开源模块
    • 自定义模块
posted @ 2019-05-23 22:07  飞琼君  阅读(84)  评论(0编辑  收藏  举报