zoukankan      html  css  js  c++  java
  • Python 学习笔记(6)--常用模块(2)

    一、下载安装

    下载安装有两种方式:

    yumpipapt-get

    或者源码

    下载源码
    解压源码
    进入目录
    编译源码    python setup.py build
    安装源码    python setup.py install

    注:在使用源码安装时,需要使用到gcc编译和python开发环境,所以,需要先执行:

    yum install gcc python-devel

    安装成功后,模块会自动安装到 sys.path 中的某个目录中,如:
    /usr/lib/python2.7/site-packages/

    time&datetime

     1 import time
     2 
     3 # print(time.clock()) #返回处理器时间
     4 # print(time.altzone)  #返回与utc时间的时间差,以秒计算
     5 # print(time.asctime()) #返回时间格式"Fri Aug 19 11:14:16 2016"
     6 # print(time.localtime()) #返回本地时间 的struct time对象格式
     7 # print(time.gmtime(time.time()-800000)) #返回utc时间的struc时间对象格式
     8 # print(time.asctime(time.localtime())) #返回时间格式"Fri Aug 19 11:14:16 2016",
     9 #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
    10 
    11 
    12 # 日期字符串 转成  时间戳
    13 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
    14 # print(string_2_struct)
    15 # struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
    16 # print(struct_2_stamp)
    17 
    18 #将时间戳转为字符串格式
    19 # print(time.gmtime(time.time()-86640)) #将utc时间戳转换成struct_time格式
    20 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
    21 
    22 
    23 #时间加减
    24 import datetime
    25 
    26 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
    27 #print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
    28 # print(datetime.datetime.now() )
    29 # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
    30 # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
    31 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
    32 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
    33 
    34 
    35 # c_time  = datetime.datetime.now()
    36 # print(c_time.replace(minute=3,hour=2)) #时间替换

    python中时间日期格式化符号:

    • %y 两位数的年份表示(00-99)
    • %Y 四位数的年份表示(000-9999)
    • %m 月份(01-12)
    • %d 月内中的一天(0-31)
    • %H 24小时制小时数(0-23)
    • %I 12小时制小时数(01-12)
    • %M 分钟数(00=59)
    • %S 秒(00-59)
    • %a 本地简化星期名称
    • %A 本地完整星期名称
    • %b 本地简化的月份名称
    • %B 本地完整的月份名称
    • %c 本地相应的日期表示和时间表示
    • %j 年内的一天(001-366)
    • %p 本地A.M.或P.M.的等价符
    • %U 一年中的星期数(00-53)星期天为星期的开始
    • %w 星期(0-6),星期天为星期的开始
    • %W 一年中的星期数(00-53)星期一为星期的开始
    • %x 本地相应的日期表示
    • %X 本地相应的时间表示
    • %Z 当前时区的名称
    • %% %号本身

    Python函数用一个元组装起来的9组数字处理时间:

    序号字段属性
    0 4位数年 tm_year 2008
    1 tm_mon 1 到 12
    2 tm_mday 1到31
    3 小时 tm_hour 0到23
    4 分钟 tm_min 0到59
    5 tm_sec 0到61 (60或61 是闰秒)
    6 一周的第几日 tm_wday 0到6 (0是周一)
    7 一年的第几日 tm_yday 1到366 (儒略历)
    8 夏令时 tm_isdst -1, 0, 1, -1是决定是否为夏令时的旗帜

     

     

     

     

     

     

     

    Time 模块包含了以下内置函数,既有时间处理相的,也有转换时间格式的:

     1 time.altzone 返回格林威治西部的夏令时地区的偏移秒数。如果该地区在格林威治东部会返回负值(如西欧,包括英国)。对夏令时启用地区才能使用。
     2 time.asctime([tupletime]) 接受时间元组并返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。
     3 time.clock( ) 以浮点数计算的秒数返回当前的CPU时间。用来衡量不同程序的耗时,比time.time()更有用。
     4 time.ctime([secs]) 作用相当于asctime(localtime(secs)),未给参数相当于asctime()
     5 time.gmtime([secs]) 接收时间辍(1970纪元后经过的浮点秒数)并返回格林威治天文时间下的时间元组t。注:t.tm_isdst始终为0
     6 time.localtime([secs]) 接收时间辍(1970纪元后经过的浮点秒数)并返回当地时间下的时间元组t(t.tm_isdst可取0或1,取决于当地当时是不是夏令时)。
     7 time.mktime(tupletime) 接受时间元组并返回时间辍(1970纪元后经过的浮点秒数)。
     8 time.sleep(secs) 推迟调用线程的运行,secs指秒数。
     9 time.strftime(fmt[,tupletime]) 接收以时间元组,并返回以可读字符串表示的当地时间,格式由fmt决定。
    10 time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') 根据fmt的格式把一个时间字符串解析为时间元组。
    11 time.time( ) 返回当前时间的时间戳(1970纪元后经过的浮点秒数)。
    12 time.tzset() 根据环境变量TZ重新初始化时间相关设置。

    time模块的重要属性

      time.timezone
      属性time.timezone是当地时区(未启动夏令时)距离格林威治的偏移秒数(>0,美洲;<=0大部分欧洲,亚洲,非洲)。

      time.tzname
      属性time.tzname包含一对根据情况的不同而不同的字符串,分别是带夏令时的本地时区名称,和不带的。

    时间转换的过程

    182222117511121.png

    random模块

    生成随机数的模块

     1 import random
     2 print(random.random())
     3 print(random.randint(1,2))
     4 print(random.randrange(1,10))
     5 
     6 生成随机验证码
     7 import random
     8 checkcode = ''
     9 for i in range(4):
    10     current = random.randrange(0,4)
    11     if current != i:
    12         temp = chr(random.randint(65,90))
    13     else:
    14         temp = random.randint(0,9)
    15     checkcode += str(temp)
    16 print checkcode

    shutil 模块

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

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

    1 拷贝部分代码的示例
    2 def copyfileobj(fsrc, fdst, length=16*1024):
    3     """copy data from file-like object fsrc to file-like object fdst"""
    4     while 1:
    5         buf = fsrc.read(length)
    6         if not buf:
    7             break
    8         fdst.write(buf)

    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.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对象

    ConfigParser

     1 # 注释1
     2 ; 注释2
     3  
     4 [section1]
     5 k1 = v1
     6 k2:v2
     7  
     8 [section2]
     9 k1 = v1
    10 
    11 import ConfigParser
    12  
    13 config = ConfigParser.ConfigParser()
    14 config.read('i.cfg')
    15  
    16 # ########## 读 ##########
    17 #secs = config.sections()
    18 #print secs
    19 #options = config.options('group2')
    20 #print options
    21  
    22 #item_list = config.items('group2')
    23 #print item_list
    24  
    25 #val = config.get('group1','key')
    26 #val = config.getint('group1','key')
    27  
    28 # ########## 改写 ##########
    29 #sec = config.remove_section('group1')
    30 #config.write(open('i.cfg', "w"))
    31  
    32 #sec = config.has_section('wupeiqi')
    33 #sec = config.add_section('wupeiqi')
    34 #config.write(open('i.cfg', "w"))
    35  
    36  
    37 #config.set('group2','k1',11111)
    38 #config.write(open('i.cfg', "w"))
    39  
    40 #config.remove_option('group2','age')
    41 #config.write(open('i.cfg', "w"))

    shelve 模块

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

    import shelve
     
    d = shelve.open('shelve_test') #打开一个文件
    
     
    t = 123
    t2 = 123334
     
    name = [,"rain","test"]
    d["test"] = name #持久化列表
    d["t1"] = t      #持久化类
    d["t2"] = t2
     
    d.close()

    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协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml

     1 import xml.etree.ElementTree as ET
     2  
     3 tree = ET.parse("xmltest.xml")
     4 root = tree.getroot()
     5 print(root.tag)
     6  
     7 #遍历xml文档
     8 for child in root:
     9     print(child.tag, child.attrib)
    10     for i in child:
    11         print(i.tag,i.text)
    12  
    13 #只遍历year 节点
    14 for node in root.iter('year'):
    15     print(node.tag,node.text)

    修改和删除xml文档内容

     1 import xml.etree.ElementTree as ET
     2  
     3 tree = ET.parse("xmltest.xml")
     4 root = tree.getroot()
     5  
     6 #修改
     7 for node in root.iter('year'):
     8     new_year = int(node.text) + 1
     9     node.text = str(new_year)
    10     node.set("updated","yes")
    11  
    12 tree.write("xmltest.xml")
    13  
    14  
    15 #删除node
    16 for country in root.findall('country'):
    17    rank = int(country.find('rank').text)
    18    if rank > 50:
    19      root.remove(country)
    20  
    21 tree.write('output.xml')

    自己创建xml文档

     1 import xml.etree.ElementTree as ET
     2 
     3 new_xml = ET.Element("namelist")
     4 name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
     5 age = ET.SubElement(name,"age",attrib={"checked":"no"})
     6 sex = ET.SubElement(name,"sex")
     7 sex.text = '33'
     8 name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
     9 age = ET.SubElement(name2,"age")
    10 age.text = '19'
    11  
    12 et = ET.ElementTree(new_xml) #生成文档对象
    13 et.write("test.xml", encoding="utf-8",xml_declaration=True)
    14  
    15 ET.dump(new_xml) #打印生成的格式

    re 模块

    re模块用于对python的正则表达式的操作。

    字符:

      . 匹配除换行符以外的任意字符
      w 匹配字母或数字或下划线或汉字
      s 匹配任意的空白符
      d 匹配数字
       匹配单词的开始或结束
      ^ 匹配字符串的开始
      $ 匹配字符串的结束

    次数:

      * 重复零次或更多次
      + 重复一次或更多次
      ? 重复零次或一次
      {n} 重复n次
      {n,} 重复n次或更多次
      {n,m} 重复n到m次

    1、match(pattern, string, flags=0)

    从起始位置开始根据模型去字符串中匹配指定内容,匹配单个

    • 正则表达式
    • 要匹配的字符串
    • 标志位,用于控制正则表达式的匹配方式

    2、search(pattern, string, flags=0)

    根据模型去字符串中匹配指定内容,匹配单个

    3、group和groups

    1 import re
    2 a = "123abc456"
    3 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group()
    4 
    5 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(0)
    6 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(1)
    7 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).group(2)
    8 
    9 print re.search("([0-9]*)([a-z]*)([0-9]*)", a).groups()

    4、findall(pattern, string, flags=0)

    上述两中方式均用于匹配单值,即:只能匹配字符串中的一个,如果想要匹配到字符串中所有符合条件的元素,则需要使用 findall。

    5、sub(pattern, repl, string, count=0, flags=0)

    用于替换匹配的字符串

    相比于str.replace功能更加强大

    1 content = "123abc456"
    2 new_content = re.sub('d+', 'sb', content)
    3 # new_content = re.sub('d+', 'sb', content, 1)
    4 print new_content

    6、split(pattern, string, maxsplit=0, flags=0)

    根据指定匹配进行分组

     1 content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
     2 new_content = re.split('*', content)
     3 # new_content = re.split('*', content, 1)
     4 print new_content
     5 
     6 content = "'1 - 2 * ((60-30+1*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2) )'"
     7 new_content = re.split('[+-*/]+', content)
     8 # new_content = re.split('*', content, 1)
     9 print new_content
    10 
    11 inpp = '1-2*((60-30 +(-40-5)*(9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2))'
    12 inpp = re.sub('s*','',inpp)
    13 new_content = re.split('(([+-*/]?d+[+-*/]?d+){1})', inpp, 1)
    14 print new_content

    hashlib

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

     1 import hashlib
     2  
     3 # ######## md5 ########
     4  
     5 hash = hashlib.md5()
     6 hash.update('admin')
     7 print hash.hexdigest()
     8  
     9 # ######## sha1 ########
    10  
    11 hash = hashlib.sha1()
    12 hash.update('admin')
    13 print hash.hexdigest()
    14  
    15 # ######## sha256 ########
    16  
    17 hash = hashlib.sha256()
    18 hash.update('admin')
    19 print hash.hexdigest()
    20  
    21  
    22 # ######## sha384 ########
    23  
    24 hash = hashlib.sha384()
    25 hash.update('admin')
    26 print hash.hexdigest()
    27  
    28 # ######## sha512 ########
    29  
    30 hash = hashlib.sha512()
    31 hash.update('admin')
    32 print hash.hexdigest()

    以上加密算法虽然依然非常厉害,但仍然存在缺陷.

    即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key再来做加密。

    1 import hashlib
    2  
    3 # ######## md5 ########
    4  
    5 hash = hashlib.md5('898oaFs09f')
    6 hash.update('admin')
    7 print hash.hexdigest()

    python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密

    1 import hmac
    2 h = hmac.new('wueiqi')
    3 h.update('hellowo')
    4 print h.hexdigest()
  • 相关阅读:
    golang sync.WaitGroup
    golang 部分理解:关于channel 和 goroutine 例子
    golang filepath.Walk遍历指定目录下的所有文件
    golang filepath.Glob
    golang 函数传值
    golang panic and recover
    golang pipe
    golang 获取指定目录下的子文件列表
    eclipse:failed to create the java virtual machine
    如何在股市中捕捉涨停
  • 原文地址:https://www.cnblogs.com/wayde-Z/p/6080285.html
Copyright © 2011-2022 走看看