本节大纲:

  1. 模块介绍
  2. time &datetime模块
  3. random
  4. shutil
  5. shelve
  6. xml处理
  7. yaml处理
  8. configparser
  9. hashlib
  10. subprocess
  11. logging模块
  12. re正则表达式

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

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

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

模块分为三种:

  • 自定义模块
  • 内置标准模块(又称标准库)
  • 开源模块

自定义模块 和开源模块的使用参考 http://www.cnblogs.com/wupeiqi/articles/4963027.html 

 

time & datetime模块

 1 #_*_coding:utf-8_*_
 2 __author__ = 'Alex Li'
 3 
 4 import time
 5 
 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 # 日期字符串 转成  时间戳
17 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
18 # print(string_2_struct)
19 # #
20 # struct_2_stamp = time.mktime(string_2_struct) #将struct时间对象转成时间戳
21 # print(struct_2_stamp)
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 #时间加减
30 import datetime
31 
32 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
33 #print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
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分
39 
40 
41 #
42 # c_time  = datetime.datetime.now()
43 # print(c_time.replace(minute=3,hour=2)) #时间替换
Directive Meaning Notes
%a Locale’s abbreviated weekday name.  
%A Locale’s full weekday name.  
%b Locale’s abbreviated month name.  
%B Locale’s full month name.  
%c Locale’s appropriate date and time representation.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
%Z Time zone name (no characters if no time zone exists).  
%% A literal '%' character.

 python-其他常用模块

 

random模块

随机数

1 mport random
2 print random.random()
3 print random.randint(1,2)
4 print random.randrange(1,10)

生成随机验证码

 1 import random
 2 checkcode = ''
 3 for i in range(4):
 4     current = random.randrange(0,4)
 5     if current != i:
 6         temp = chr(random.randint(65,90))
 7     else:
 8         temp = random.randint(0,9)
 9     checkcode += str(temp)
10 print checkcode

shutil 模块

直接参考 http://www.cnblogs.com/wupeiqi/articles/4963027.html 

shelve 模块

 1 import shelve
 2  
 3 d = shelve.open('shelve_test') #打开一个文件
 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  
13 name = ["alex","rain","test"]
14 d["test"] = name #持久化列表
15 d["t1"] = t      #持久化类
16 d["t2"] = t2
17  
18 d.close()

xml处理模块

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

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

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
View Code

相关文章:

  • 2021-08-08
  • 2022-12-23
  • 2020-04-29
  • 2023-01-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-06
猜你喜欢
  • 2021-11-05
  • 2021-04-05
  • 2021-11-08
  • 2021-06-01
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
相关资源
相似解决方案