• python之路第五篇


    python中常用的模块

    模块介绍

    模块,用一砣代码实现了某个功能的代码集合。 

    类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

    如:os 是系统相关的模块;file是文件操作相关的模块

    模块分为三种:

    • 自定义模块
    • 内置标准模块(又称标准库)
    • 开源模块
    time&&datetime模块介绍

    在Python中,通常有这几种方式来表示时间:1)时间戳 2)格式化的时间字符串 3)元组(struct_time)共九个元素。由于Python的time模块实现主要调用C库,所以各个平台可能有所不同。


    UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。

    时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。

    元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。下面列出这种方式元组中的几个元素:

     1 #-*-coding:utf-8-*-
     2 #!/usr/bin/env python
     3 # __author__ = 'mengxj'
     4 
     5 import time
     6 print(time.clock()) #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
     7 print(time.altzone)  #返回与utc时间的时间差,以秒计算
     8 print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016",
     9 print(time.localtime()) #返回本地时间 的struct time对象格式
    10 print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
    11 
    12 print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016",
    13 print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
    14 
    15 # 日期字符串 转成  时间戳
    16 struct_time = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
    17 print(struct_time)
    18 #
    19 struct_time2 = time.mktime(struct_time) #将struct时间对象转成时间戳
    20 print(struct_time2)
    21 
    22 
    23 
    24 #将时间戳转为字符串格式
    25 print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
    26 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
    27 
    28 #时间加减
    29 import datetime
    30 
    31 print("当前时间:",datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
    32 print("时间戳转换成日期:",datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
    33 print("时间戳时间:",datetime.date.fromtimestamp(2016))
    34 print("当前时间:",datetime.datetime.now() )
    35 print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
    36 print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
    37 print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
    38 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
    time&datatime

     时间关系转换

    random
     1 #-*-coding:utf-8-*-
     2 #!/usr/bin/env python
     3 # __author__ = 'mengxj'
     4 
     5 import random
     6 print (random.random())  #0.6445010863311293
     7 #random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0
     8 print (random.randint(1,7)) #用于生成1-7之间随机的数字;
     9 #random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。
    10 # 其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
    11 print (random.randrange(1,10)) #5
    12 #random.randrange的函数原型为:random.randrange([start], stop[, step]),
    13 # 从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),
    14 # 结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。
    15 # random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。
    16 print(random.choice('liukuni')) #i
    17 #random.choice从序列中获取一个随机元素。
    18 # 其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。
    19 # 这里要说明一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。
    20 # list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章。
    21 # 下面是使用choice的一些例子:
    22 print("学习Python随机产生:",random.choice("学习Python"))#
    23 print(random.choice(["list随机:"+"JGood","is","a","handsome","boy"]))  #List
    24 print("tuple随机产生:",random.choice(("Tuple","List","Dict")))   #List
    25 print("sample函数随机产生:",random.sample([1,2,3,4,5],3))    #[1, 2, 5]
    26 #random.sample的函数原型为:random.sample(sequence, k),从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。

     实际应用

     1 import random
     2 import string
     3 #随机整数:
     4 print( random.randint(0,99))
     5 
     6 #随机选取0到100间的偶数:
     7 print(random.randrange(0, 101, 2))
     8 
     9 #随机浮点数:
    10 print( random.random())
    11 print(random.uniform(1, 10))
    12 
    13 #随机字符:
    14 print(random.choice('abcdefg&#%^*f')) #f
    15 
    16 #多个字符中选取特定数量的字符:
    17 print(random.sample('abcdefghij',3)) #['f', 'h', 'd']
    18 
    19 #随机选取字符串:
    20 print( random.choice ( ['apple', 'pear', 'peach', 'orange', 'lemon'] )) #apple
    21 #洗牌#
    22 items = [1,2,3,4,5,6,7]
    23 print(items) #[1, 2, 3, 4, 5, 6, 7]
    24 random.shuffle(items)   #shuffle对数据进行洗牌
    25 print(items) #[1, 4, 7, 2, 5, 3, 6]

    random程序小练习

     1 #random程序练习
     2 import random
     3 checkcode = ''
     4 for i in range(0,4):
     5     current = random.randrange(0,4)
     6     print(current)
     7     #print("i=",i)
     8     if current !=i:
     9         temp = chr(random.randint(65,90))   #65-90 为字母A-Z的范围
    10     else:
    11         temp =random.randint(0,9)
    12     checkcode +=str(temp)
    13 print(checkcode)
    OS模块
     1 import os
     2 os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径
     3 #os.chdir("dirname")  #改变当前脚本工作目录;相当于shell下cd
     4 os.curdir  #返回当前目录: ('.')
     5 os.pardir  #获取当前目录的父目录字符串名:('..')
     6 #os.makedirs('dirname1/dirname2')    #可生成多层递归目录
     7 #os.removedirs('dirname1')   # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
     8 os.mkdir('dirname')    #生成单级目录;相当于shell中mkdir dirname
     9 os.rmdir('dirname')   # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
    10 #os.listdir('dirname')    #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
    11 #os.remove()  #删除一个文件
    12 #os.rename("oldname","newname")  #重命名文件/目录
    13 #os.stat('path/filename')  #获取文件/目录信息
    14 os.sep    #输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
    15 os.linesep    #输出当前平台使用的行终止符,win下为"	
    ",Linux下为"
    "
    16 os.pathsep    #输出用于分割文件路径的字符串
    17 os.name    #输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
    18 os.system("bash command")  #运行shell命令,直接显示
    19 os.environ  #获取系统环境变量
    20 #os.path.abspath()  #返回path规范化的绝对路径
    21 #os.path.split()  #将path分割成目录和文件名二元组返回
    22 #os.path.dirname()  #返回path的目录。其实就是os.path.split(path)的第一个元素
    23 #os.path.basename()  #返回path最后的文件名。如何path以/或结尾,那么就会返回空值。即os.path.split(path)的第二个元素
    24 #os.path.exists()  #如果path存在,返回True;如果path不存在,返回False
    25 # os.path.isabs()  #如果path是绝对路径,返回True
    26 # os.path.isfile()  #如果path是一个存在的文件,返回True。否则返回False
    27 # os.path.isdir()  #如果path是一个存在的目录,则返回True。否则返回False
    28 # #os.path.join(path1[, path2[, ...]])  #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
    29 # os.path.getatime()  #返回path所指向的文件或者目录的最后存取时间
    30 # os.path.getmtime()  #返回path所指向的文件或者目录的最后修改时间
    sys模块
    1 import sys
    2 sys.argv           命令行参数List,第一个元素是程序本身路径
    3 sys.exit(n)        退出程序,正常退出时exit(0)
    4 sys.version        获取Python解释程序的版本信息
    5 sys.maxint         最大的Int值
    6 sys.path           返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
    7 sys.platform       返回操作系统平台名称
    8 sys.stdout.write('please:')
    9 val = sys.stdin.readline()[:-1]
    Json&pickle

    用于序列化的两个模块

    • json,用于字符串 和 python数据类型间进行转换
    • pickle,用于python特有的类型 和 python的数据类型间进行转换

    Json模块提供了四个功能:dumps、dump、loads、load

    pickle模块提供了四个功能:dumps、dump、loads、load

    data =str({"k1":123,2:"hello"})
    data1 ="hello"
    # print("strstrrrrrr",type(data))
    #pickle.dumps 将数据通过特殊的形式转化为只有python语言认识的字符串;
    # p_str = pickle.dumps(data)
    # print(p_str)
    #pickle.dump 将数据通过特殊的形式转换为只有python语言认识 的字符串,并写入文件
    # with open("D:/OneDrive/python_code/python_s14/s14_day5/str_pickle","wb") as f:
    #     pickle.dump(data1,f)
    # f =open("D:/OneDrive/python_code/python_s14/s14_day5/str_pickle","w")
    # # f.write(data)
    1 import json
    2 #json.jumps 将数据通过特殊的形式转换为所有程序都认识的字符串;
    3 j_str = json.dumps(data)
    4 print(j_str)
    5 
    6 #json.dump将数据通过特殊的形式转换成所有程序语言都认识的字符串,并写入文件;
    7 with open("D:/OneDrive/python_code/python_s14/s14_day5/str_pickle","w") as f:
    8     json.dump(data,f)
    shelve模块

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

     1 import shelve
     2 
     3 d = shelve.open('shelve_test1.txt') #打开一个文件
     4 
     5 class Test(object):
     6     def __init__(self,n):
     7         self.n = n
     8 
     9 
    10 t = Test(123)
    11 t2 = Test(123334)
    12 print(t)
    13 print(t2)
    14 
    15 name = ["alex","rain","test"]
    16 d["test"] = name #持久化列表
    17 d["t1"] = t      #持久化类
    18 d["t2"] = t2
    19 
    20 d.close()
    21 
    22 运行结果:
    23 'test', (0, 41)
    24 't2', (1024, 45)
    25 't1', (512, 42)
    XML

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

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

     1 <?xml version="1.0"?>
     2 <data>
     3     <country name="Liechtenstein">
     4         <rank updated="yes">2</rank>
     5         <year>2008</year>
     6         <gdppc>141100</gdppc>
     7         <neighbor name="Austria" direction="E"/>
     8         <neighbor name="Switzerland" direction="W"/>
     9     </country>
    10     <country name="Singapore">
    11         <rank updated="yes">5</rank>
    12         <year>2011</year>
    13         <gdppc>59900</gdppc>
    14         <neighbor name="Malaysia" direction="N"/>
    15     </country>
    16     <country name="Panama">
    17         <rank updated="yes">69</rank>
    18         <year>2011</year>
    19         <gdppc>13600</gdppc>
    20         <neighbor name="Costa Rica" direction="W"/>
    21         <neighbor name="Colombia" direction="E"/>
    22     </country>
    23 </data>
    xml范本

    操作xml

     1 import xml.etree.ElementTree as ET
     2 
     3 tree =ET.parse("xml_ex.xml")
     4 root = tree.getroot()
     5 print(root)
     6 print(root.tag)
     7 
     8 for child in root:
     9     print(child.tag,child.attrib)
    10     print("----------------------")
    11     for i in child:
    12         print(i.tag,i.text)
    Configparser模块

    用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser。

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

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

    要让python自动生成一个这样的文档,怎么办呢?

     1 import configparser
     2  
     3 config = configparser.ConfigParser()
     4 config["DEFAULT"] = {'ServerAliveInterval': '45',
     5                       'Compression': 'yes',
     6                      'CompressionLevel': '9'}
     7  
     8 config['bitbucket.org'] = {}
     9 config['bitbucket.org']['User'] = 'hg'
    10 config['topsecret.server.com'] = {}
    11 topsecret = config['topsecret.server.com']
    12 topsecret['Host Port'] = '50022'     # mutates the parser
    13 topsecret['ForwardX11'] = 'no'  # same here
    14 config['DEFAULT']['ForwardX11'] = 'yes'
    15 with open('example.ini', 'w') as configfile:
    16    config.write(configfile)
    hashlib
    1 import hashlib
    2 m = hashlib.md5()
    3 m.update(b"hello")
    4 print(m.hexdigest())
    5 m.update(b"good")
    6 print("m is",m)
    7 print(m.hexdigest())
    8 m.update("我放假".encode(encoding="utf-8"))
    9 print(m.hexdigest())
    re正则表达式模块
     1 '.'     默认匹配除
    之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
     2 '^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","
    abc
    eee",flags=re.MULTILINE)
     3 '$'     匹配字符结尾,或e.search("foo$","bfoo
    sdfsf",flags=re.MULTILINE).group()也可以
     4 '*'     匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']
     5 '+'     匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
     6 '?'     匹配前一个字符1次或0次
     7 '{m}'   匹配前一个字符m次
     8 '{n,m}' 匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
     9 '|'     匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
    10 '(...)' 分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c
    11  
    12  
    13 'A'    只从字符开头匹配,re.search("Aabc","alexabc") 是匹配不到的
    14 ''    匹配字符结尾,同$
    15 'd'    匹配数字0-9
    16 'D'    匹配非数字
    17 'w'    匹配[A-Za-z0-9]
    18 'W'    匹配非[A-Za-z0-9]
    19 's'     匹配空白字符、	、
    、
     , re.search("s+","ab	c1
    3").group() 结果 '	'
    20  
    21 '(?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'}

    最最常用的匹配语法

    1 re.match 从头开始匹配
    2 re.search 匹配包含
    3 re.findall 把所有匹配到的字符放到以列表中的元素返回
    4 re.splitall 以匹配到的字符当做列表分隔符
    5 re.sub      匹配字符并替换
  • 相关阅读:
    v4v7升级到androidx过程
    iOS13 ,xcode11新建项目真机运行出现黑屏
    逆向微信时使用runtime方法交换后因找不到方法而崩溃的解决方式
    block不需要__weak的几种情况
    使用Xcode 11.3bata版本创建的工程webview无法加载http
    iOS 隐藏/显示状态栏
    iOS开发之cell中self和self.contentview的区别
    栈是吃了吐 堆是吃了拉
    工厂方法
    NSNotification、KVO、Delegate都是同步的
  • 原文地址:https://www.cnblogs.com/bravexz/p/5802181.html
Copyright © 2020-2023  润新知