Python17個常用內置模塊總結


<--目錄-->

1、getpass

2、os

3、sys

4、subprocess

5、hashlib

6、json

7、pickle

8、shutil

9、time

10、datetime

11、re

12、random

13、configparser

14、traceback

15、yaml

16、itertools 

17、logging

 

1、getpass模塊詳解

pwd = getpass.getpass("請輸入密碼:")  #輸入密碼不可見

yh = getpass.getuser()                #顯示當前登錄系統用戶名;

 

2、os模塊(與操作系統交互)

os.getcwd()                           #獲取當前工作目錄,即當前python腳本工作的目錄路徑

os.chdir("dirname")                   #改變當前腳本工作目錄;相當於shell下cd

os.curdir                             #返回當前目錄: ('.')

os.pardir                             #獲取當前目錄的父目錄字符串名:('..')

os.makedirs('dirname1/dirname2')      #可生成多層遞歸目錄

os.removedirs('dirname1')             #若目錄為空,則刪除,並遞歸到上一級目錄,如若也為空,則刪除,依此類推

os.mkdir('dirname')                   #生成單級目錄;相當於shell中mkdir dirname

os.rmdir('dirname')                   #刪除單級空目錄,若目錄不為空則無法刪除,報錯;相當於shell中rmdir dirname

os.listdir('dirname')                 #列出指定目錄下的所有文件和子目錄,包括隱藏文件,並以列表方式打印

os.remove()                           #刪除一個文件

os.rename("oldname","newname")        #重命名文件/目錄

os.stat('path/filename')              #獲取文件/目錄信息

os.sep                                #輸出操作系統特定的路徑分隔符,win下為"\\",Linux下為"/"

os.linesep                            #輸出當前平台使用的行終止符,win下為"\t\n",Linux下為"\n"

os.pathsep                            #輸出用於分割文件路徑的字符串

os.name                               #輸出字符串指示當前使用平台。win->'nt'; Linux->'posix'

os.system("bash command")             #運行shell命令,直接顯示

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最后的文件名。如何path以/或\結尾,那么就會返回空值。即os.path.split(path)的第二個元素

os.path.exists(path)                  #如果path存在,返回True;如果path不存在,返回False

os.path.isabs(path)                   #如果path是絕對路徑,返回True

os.path.isfile(path)                  #如果path是一個存在的文件,返回True。否則返回False

os.path.isdir(path)                   #如果path是一個存在的目錄,則返回True。否則返回False

os.path.join(path1[, path2[, ...]])   #將多個路徑組合后返回,第一個絕對路徑之前的參數將被忽略

os.path.getatime(path)                #返回path所指向的文件或者目錄的最后存取時間

os.path.getmtime(path)                #返回path所指向的文件或者目錄的最后修改時間
View Code

 

# 獲取當前文件(或文件夾)所在的路徑
CUR_PATH = os.path.dirname(__file__)
# 路徑拼接:拼接文件的’絕對路徑‘
TEST_PATH = os.path.join(CUR_PATH,'test.txt')
# 判斷文件或文件夾是否存在
res = os.path.exists(TEST_PATH)
# 判斷文件夾是否存在
res = os.path.isdir(TEST_PATH})
# 創建文件夾
os.mkdir(DIR_PATH)
# 刪除文件夾
os.rmdir(DIR_PATH)
# 刪除文件
os.remove(file_name)
# 獲取指定文件夾下面的所有文件夾名和文件名
os.listdir(DIR_PATH)

 

 

3、sys模塊(與python解釋器交互)

sys.argv                              #命令行參數List,第一個元素是程序本身路徑

sys.exit(n)                           #退出程序,正常退出時exit(0)

sys.version                           #獲取Python解釋程序的版本信息

sys.maxint                            #最大的Int值

sys.path                              #返回模塊的搜索路徑,初始化時使用PYTHONPATH環境變量的值

sys.platform                          #返回操作系統平台名稱

sys.stdout.write('please:')

val = sys.stdin.readline()[:-1]
# 獲取當前的python解釋器的環境變量
sys.path
# 將當前項目添加到環境變量中
BASE_PATH = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_PATH)
# 獲取cmd終端命令,以空格分割,返回列表
print(sys.argv)
View Code

 

 

 

4、subprocess模塊  執行系統命令 

os.system

commands.*      --廢棄,3.x中被移除

result = commands.getoutput('cmd')

以上執行shell命令的相關的模塊和函數的功能均在 subprocess 模塊中實現,並提供了更豐富的功能。

 

call 

執行命令,返回狀態碼

ret = subprocess.call(["ls", "-l"], shell=False)

ret = subprocess.call("ls -l", shell=True)

shell = True ,允許 shell 命令是字符串形式

 

check_call

執行命令,如果執行狀態碼是 0 ,則返回0,否則拋異常

subprocess.check_call(["ls", "-l"])

subprocess.check_call("exit 1", shell=True)

 

check_output(此下兩條命令在2.6執行失敗,要是2.7應該才可以)

執行命令,如果狀態碼是 0 ,則返回執行結果,否則拋異常

subprocess.check_output(["echo", "Hello World!"])

subprocess.check_output("exit 1", shell=True)

 

subprocess.Popen(...)

用於執行復雜的系統命令

參數:

args:                      #shell命令,可以是字符串或者序列類型(如:list,元組)

bufsize:                   #指定緩沖。0 無緩沖,1 行緩沖,其他 緩沖區大小,負值 系統緩沖

stdin, stdout, stderr:     #分別表示程序的標准輸入、輸出、錯誤句柄

preexec_fn:                #只在Unix平台下有效,用於指定一個可執行對象(callable object),它將在子進程運行之前被調用

close_sfs:                 #在windows平台下,如果close_fds被設置為True,則新創建的子進程將不會繼承父進程的輸入、輸出、錯誤管道。所以不能將close_fds設置為True同時重定向子進程的標准輸入、輸出與錯誤(stdin, stdout, stderr)。

shell:                     #同上

cwd:                       #用於設置子進程的當前目錄

env:                       #用於指定子進程的環境變量。如果env = None,子進程的環境變量將從父進程中繼承。

universal_newlines:        #不同系統的換行符不同,True -> 同意使用 \n

startupinfo與createionflags #只在windows下有效

將被傳遞給底層的CreateProcess()函數,用於設置子進程的一些屬性,如:主窗口的外觀,進程的優先級等等

 

import subprocess

ret1 = subprocess.Popen(["mkdir","t1"])

ret2 = subprocess.Popen("mkdir t2", shell=True)
View Code

 

5、hashlib模塊

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

import hashlib
# ######## 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()

以上加密算法雖然依然非常厲害,但時候存在缺陷,即:通過撞庫可以反解。所以,有必要對加密算法中添加自定義key再來做加密。

import hashlib

# ######## md5 ########

hash = hashlib.md5('898oaFs09f')

hash.update('admin')

print hash.hexdigest()

 
還不夠吊?python 還有一個 hmac 模塊,它內部對我們創建 key 和 內容 再進行處理然后再加密

import hmac

h = hmac.new('wueiqi')

h.update('hellowo')

print h.hexdigest()
View Code

 

import hashlib

def pwd_md5(pwd):
    md5_obj = hashlib.md5()
      md5_obj.update(pwd.encode('utf-8'))
    var = '寶塔鎮河妖'
    md5_obj.update(val.encode('utf-8'))
    pwd = md5_obj.hexdigest()
    return pwd

 

 

6,7、json 和 pickle 

用於序列化的兩個模塊

用於序列化的兩個模塊

json,用於字符串 和 python數據類型間進行轉換

pickle,用於python特有的類型 和 python的數據類型間進行轉換

Json模塊提供了四個功能:dumps、dump、loads、load

pickle模塊提供了四個功能:dumps、dump、loads、load

 

pickle

>>> import pickle

>>> data = {'k1' : 123, 'k2' : 'hello'}

>>> p_str = pickle.dumps(data)   #序列化

>>> print p_str  

>>> loadsed = pickle.loads(p_str)   #反序列化

>>> print loadsed

 

序列化到文件

>>> li = ['wsyht',11,22,'ok','yes']

>>> pickle.dump(li,open('test.txt','w'))   #序列化到文件 

>>> pickle.load(open('test.txt'))          #從文件反序列化出來

 

json

>>> import json

>>> data = {'k1':123,'k2':'abc'}

>>> str = json.dumps(data)

>>> stt= json.loads(str)  

 

序列化到文件

>>> li = ['wsyht',11,22,'ok','yes'] 

>>> json.dump(li,open('test.txt','w'))  #序列化到文件

>>> json.load(open('test.txt'))         #從文件反序化出來
View Code

 

8、shutil模塊

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對象

 

#將 /Users/wupeiqi/Downloads/test 下的文件打包放置當前程序目錄

 

import shutil

ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

 

 

#將 /mnt下的文件打包放置 /tmp目錄

import shutil

ret = shutil.make_archive("/tmp/www", 'gztar', root_dir='/mnt')  #2.6用不了,2.7或許可以

 

 

類似於高級API,而且主要強大之處在於其對文件的復制與刪除操作更是比較支持好。

 

相關API介紹

 

copyfile(src, dst)

從源src復制到dst中去。當然前提是目標地址是具備可寫權限。拋出的異常信息為

 

IOException. 如果當前的dst已存在的話就會被覆蓋掉。

 

 

 copyfile( src, dst)  從源src復制到dst中去。當然前提是目標地址是具備可寫權限。拋出的異常信息為IOException. 如果當前的dst已存在的話就會被覆蓋掉

 copymode( src, dst)  只是會復制其權限其他的東西是不會被復制的

 copystat( src, dst)  復制權限、最后訪問時間、最后修改時間

 copy( src, dst)     復制一個文件到一個文件或一個目錄

 copy2( src, dst)   在copy上的基礎上再復制文件最后訪問時間與修改時間也復制過來了,類似於cp –p的東西

 copy2( src, dst)   如果兩個位置的文件系統是一樣的話相當於是rename操作,只是改名;如果是不在相同的文件系統的話就是做move操作

 copytree(olddir,newdir,True/Flase)  把olddir拷貝一份newdir,如果第3個參數是True,則復制目錄時將保持文件夾下的符號連接,如果第3個參數是False,則將在復制的目錄下生成物理副本來替代符號連接

 shutil.rmtree("te")      刪除一個目錄

 

 

import shutil

shutil.copyfile('f:/temp.txt', 'f:/os.txt') #復制文件

shutil.copytree('f:/temp', 'f:/os')  #復制目錄

 

 

# ######## zip的用法 ########

 

shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:

 

import zipfile

 

# 壓縮

z = zipfile.ZipFile('laxi.zip', 'w')

z.write('a.log')        #壓縮包寫入a.log

z.write('data.data')    #寫入data文件

z.close()

 

# 解壓

z = zipfile.ZipFile('laxi.zip', 'r')

z.extractall()

z.close()
View Code

 

9、time模塊

python中的三種時間表示形式
1、時間戳
	- 自1970-01-01 00:00:00到當前時間,單位為秒
2、格式化時間
	- 返回的是時間的字符串
3、格式化時間對象(struct_time)
	- 9個值分別代表:年、月、日、時、分、秒、一周中第幾天、一年中的第幾天、夏令時
# 1、獲取時間戳
now_time = time.time()
#==>1573885266.3314579
# 2、獲取格式化時間
now_time = time.strftime('%Y-%m-%d %H:%M:%S')
==>2019-11-16 14:21:06
# 3、獲取時間對象
time_obj = time.localtime()
print(time_obj.tm_year)
print(time_obj.tm_mon)
print(time_obj.tm_mday)
print(time_obj.tm_hour)
print(time_obj.tm_min)
print(time_obj.tm_sec)
# 4、時間對象-->字符串格式化時間
time_str=time.strftime('%Y-%m-%d %H:%M:%S',time_obj)
# 5、字符串格式化的時間-->時間對象
time_boj = time.strptime('2019-01-01','%Y-%m-%d')
View Code
三種表示主式:

1、時間戳 1970年1月1日后的秒

2、元組包含了:年、日、星期等...time.struct_time

3、格式化的字符串 2014-11-11 11:11 print time.time()

 

#時間戳形式存在

print time.time()

print time.mktime(time.localtime())  #print (time.localtime())此為元組形式,這一整句意思是把元組形式轉化成時間戳形式

 

#元組形式存在

print time.gmtime() #可加時間戳參數

print time.localtime() #可加時間戳參數

print time.strptime('2014-11-11','%Y-%m-%d') #字符串形式轉換成元組形式

 

#字符串形式存在

print time.strftime('%Y-%m-%d')  #默認當前時間,必須記住,工作中用得最多

print time.strftime('%Y-%m-%d',time.localtime())  #默認當前時間

print time.asctime()

print time.asctime(time.localtime())

print time.ctime(time.time())

 

 

時間的三種表示方式演示

>>> import time

>>> print time.time()

1469014348.5   #秒,時間戳的方式

>>> print time.gmtime()

time.struct_time(tm_year=2016, tm_mon=7, tm_mday=20, tm_hour=11, tm_min=25, tm_sec=53, tm_wday=2, tm_yday=202, tm_isdst=0)

>>> print time.strftime('%Y-%m-%d %H:%M:%S')

2016-07-20 19:36:16
View Code

 

 

 

10、datetime模塊

 

import datetime

'''

datetime.date:表示日期的類。常用的屬性有year, month, day

datetime.time:表示時間的類。常用的屬性有hour, minute, second, microsecond

datetime.datetime:表示日期時間

datetime.timedelta:表示時間間隔,即兩個時間點之間的長度

timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

strftime("%Y-%m-%d")

'''

import datetime

print datetime.datetime.now()

print datetime.datetime.now() - datetime.timedelta(days=5)
View Code
# 1、獲取當前年月日
print(datetime.date.today())
# ==>2020-04-08
# 2、獲取當前年月日時分秒
print(datetime.datetime.today())
print(datetime.datetime.now())
print(datetime.datetime.utcnow()) # 格林尼治時間
# ==>2020-04-08 14:54:34.496536
# ==>2020-04-08 14:54:34.496536
# ==>2020-04-08 06:54:34.496536
View Code
# 日期/時間的計算
#    日期時間 = 日期時間 + - 時間對象
current_time = datetime.datetime.now()
time_obj = date.timedelta(days=7)
later_time = current_time + time_obj
#    時間對象 = 日期時間 + - 日期時間
current_time = datetime.datetime.now()
later_time = datetime.datetime.utcnow()
time_obj = later_time - current_time
View Code

 

11、re模塊

compile

match search findall

group groups

正則表達式常用格式:

 

  字符:\d \w \t  . 

 

  次數:* + ? {m} {m,n}

 

示例:

#!#/usr/bin/env python

#coding:utf-8

 

import re 

result1 = re.match('\d+', '1afsfweasfcxvsfd123')  #在你給的字符串起始位置去匹配,\d從數字開始找,+表示一個到多個

if result1:   #當result1等於True的時候,就是匹配,如果匹配就輸出里面的內容

    print result1.group()  #用group方法把他匹配的內容輸出出來

else:

    print 'nothing'

 

result2 = re.search('\d+', 'alsfj3af') #在整個內容里面去匹配,\d從數字開始找,+表示一個到多個

if result2:

    print result2.group()    #用group方法把他匹配的內容輸出出來

 

result3 = re.findall('\d+', 'asfaf11sf22lj33') #只要匹配全都拿出來

print result3

 

com = re.compile('\d+')

print com.findall('asfaf11sf22lj33')

 

result5 = re.search('(\d+)\w*(\d+)','aasflsjfa12aaljsf22lj13bb')

print result5.group()   #所有匹配內容輸出

print result5.groups()  #只把括號\d,也就是組里面的內容輸出

 

result6 = re.search('a{3,5}','aaaaaa') #匹配3到5次的aaaaa輸出出來

print result6.group()
View Code
 

總結:

match:只在第一個字符串開始找,如果沒有匹配,則不再繼續找,如果第一個字符串中有,則只輸出第一個

searh: 在所有內容里找,直到找到為止,但只輸出找到的第一個

findall:把所有找到的匹配的內容,都通過列表的形式打印出來

compile: 編譯之后再去匹配,這樣可以加快匹配的速度

group: 把他匹配的內容輸出出來

groups:分組

 

匹配的字符:

\d:表示數字的意思

\w: 代表下划線,字母,數字

\t:制表符,除了回車以外的所有字符

 

匹配的次數:

* 大於等於0,0到多個

+ 大於等於1,1個到多個

?  0或1

{m} 次數,如a{6},出現6次a的進行匹配

{m,n} 如a{3,7} 出現3到7次的就進行匹配

 

例子1:

法1

>>> ip = '12.23.84.dsfa.23s.3234~lsjfw+23sfaf192.168.32.43_w342d~@#9436'

>>> import re                  

>>> re.findall('[0-9]{1,3}',ip)

['12', '23', '84', '23', '323', '4', '23', '192', '168', '32', '43', '342', '943', '6']

>>> re.findall('[0-9]{1,3}\.[0-9]{1,3}',ip)

['12.23', '192.168', '32.43']

>>> re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',ip)

['12.23.84', '192.168.32']

>>> re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}',ip)

['192.168.32.43']

 

法2:

>>> re.findall('(\d+)',ip)  

['12', '23', '84', '23', '3234', '23', '192', '168', '32', '43', '342', '9436']

>>> re.findall('(\.+\d+){1,3}',ip)

['.84', '.23', '.3234', '.43']

>>> re.findall('(?:\.+\d+){1,3}',ip)    #?:表示匹配括號的那一組數據,必須連着

['.23.84', '.23', '.3234', '.168.32.43']

>>> re.findall('[0-9]{1,3}(?:\.+\d+){3}',ip)  

['192.168.32.43']

 

法3:

>>> re.findall('(?:\d+\.+){3}\d{1,3}',ip)  

['192.168.32.43']

 

法4:

>>> re.findall('(?:\d{1,3}\.){3}\d{1,3}',ip)    

['192.168.32.43']
View Code

12、random模塊

import random
# 隨機從1-9中返回一個整數
res = random.int(1,9)
# 返回0-1之間的浮點數
res = random.random()
# shuffle(洗牌)有索引的可變可迭代對象
my_list = [1, 2, 3, 3, 7]
random.shuffle(my_list)
print(my_list)
# choice(隨機選擇)有索引的可迭代對象
my_str =  'yyh NO.1'
random.choice(my_str)
View Code
import random

print random.random()

print random.randint(1,2)

print random.randrange(1,10)

 

#隨機驗證碼實例:

import random

checkcode = ''

for i in range(4):

    current = random.randrange(0,4)

    if current != i:

        temp = chr(random.randint(65,90))

    else:

        temp = random.randint(0,9)

    checkcode += str(temp)

print checkcode

 
View Code
# 例題:生成隨機驗證碼
def random_code(n):
    char_range = [chr(i) for i in range(65, 91)] + \
                 [chr(i) for i in range(97, 123)] + \
                 [str(i) for i in range(10)]
    result = ''
    for i in range(n):
        result += random.choice(char_range)
    return result

print(random_code(6))
View Code

 

13、ConfigParser模塊

用於對特定的配置進行操作,當前模塊的名稱在 python 3.x 版本中變更為 configparser。

1.讀取配置文件

-read(filename) 直接讀取ini文件內容

-sections() 得到所有的section,並以列表的形式返回

-options(section) 得到該section的所有option

-items(section) 得到該section的所有鍵值對

-get(section,option) 得到section中option的值,返回為string類型

-getint(section,option) 得到section中option的值,返回為int類型

 

2.寫入配置文件

-add_section(section) 添加一個新的section

-set( section, option, value) 對section中的option進行設置

         需要調用write將內容寫入配置文件。

 

[root@test1 mnt]# cat data.txt 

[sec_a]

a_key1 = 20

a_key2 = 10

 

[sec_b]

b_key1 = 121

b_key2 = b_value2

b_key3 = $r

b_key4 = 127.0.0.1

 

>>> import ConfigParser

>>> cf = ConfigParser.ConfigParser()

>>> cf.read("data.txt")             

['data.txt']

>>> secs = cf.sections()  #獲得所有區域

>>> print 'sections:', secs

sections: ['sec_b', 'sec_a']

>>> opts = cf.options("sec_a")

>>> print 'options:', opts

options: ['a_key1', 'a_key2']

>>> 

>>> for sn in secs:           

...  print cf.options(sn)   #打印出每個區域的所有屬性

... 

['b_key4', 'b_key1', 'b_key2', 'b_key3']

['a_key1', 'a_key2'] 

 

>>> str_val = cf.get("sec_a", "a_key1")

>>> int_val = cf.getint("sec_a", "a_key2")

>>> print "value for sec_a's a_key1:", str_val

value for sec_a's a_key1: 20

>>> print "value for sec_a's a_key2:", int_val

value for sec_a's a_key2: 10

 

>>> cf.set("sec_b", "b_key3", "new-$r")

>>> cf.set("sec_b", "b_newkey", "new-value")

>>> cf.add_section('a_new_section')

>>> cf.set('a_new_section', 'new_key', 'new_value')

>>> cf.write(open("data.txt", "w")) 

 

>>> cf.has_section('a_new_section')  #判斷存不存在[sec_a]                 

True

>>> cf.remove_section('sec_a')   #刪除[sec_a]             

True

>>> cf.has_section('a_section')    #判斷存不存在[sec_a]      

False

>>> cf.write(open("data.txt", "w"))
View Code

 

 14、traceback模塊

[root@test1 mnt]# cat test.py 

#!/usr/bin/env python

#coding:utf-8

import traceback

try:  

    1/0  

except Exception,e:  

    #print e

    traceback.print_exc(file=open('tb.txt','w+'))

else:

    print 'success'

 
View Code

 

15、yaml模塊的使用

yaml在python上的具體實現:PyYaml

 

將yaml寫成配置腳本test.yaml ,以下介紹如何讀寫yaml配置。

 

使用python的yaml庫PyYAML。http://pyyaml.org/

 

安裝到python lib下后就可以正常使用了。

 

#加載yaml

import yaml

f = open('test.yaml')   #讀取文件

x = yaml.load(f)   #導入

print x

f.close()

 

import yaml  

f = open('d:/newtree.yaml', "w")  

yaml.dump(dataMap, f)  

f.close()  
View Code

 

 

16、itertools模塊的使用

[root@test1 mnt]# cat test.py 

 

# ######## count(1) ########

import itertools

natuals = itertools.count(1)   #count創建無限個迭代器

for n in natuals:

        print n

 

# ######## cycle() ########

 

cycle()會把傳入的一個序列無限重復下去:

>>> import itertools

>>> cs = itertools.cycle('ABC') # 注意字符串也是序列的一種

>>> for c in cs:

...     print c

 

# ######## repeat() ########

 

repeat()負責把一個元素無限重復下去,不過如果提供第二個參數就可以限定重復次數:

>>> ns = itertools.repeat('A', 10)

>>> for n in ns:

...     print n

...

打印10次'A'

 

# ######## takewhile() ########

 

>>> natuals = itertools.count(1)

>>> ns = itertools.takewhile(lambda x: x <= 10, natuals)

>>> for n in ns:

...     print n

...

打印出1到10

 

# ######## chain() ########

 

for c in itertools.chain('ABC', 'XYZ'):

    print c

# 迭代效果:'A' 'B' 'C' 'X' 'Y' 'Z'

 

# ######## groupby() ########

 

groupby()把迭代器中相鄰的重復元素挑出來放在一起:

 

>>> for key, group in itertools.groupby('AAABBBCCAAA'):

...     print key, list(group) # 為什么這里要用list()函數呢?

...

A ['A', 'A', 'A']

B ['B', 'B', 'B']

C ['C', 'C']

A ['A', 'A', 'A']

 

# ######## groupby() ########

 

實際上挑選規則是通過函數完成的,只要作用於函數的兩個元素返回的值相等,這兩個元素就被認為是在一組的,而函數返回值作為組的key。如果我們要忽略大小寫分組,就可以讓元素'A''a'都返回相同的key:

>>> for key, group in itertools.groupby('AaaBBbcCAAa', lambda c: c.upper()):

...     print key, list(group)

...

A ['A', 'a', 'a']

B ['B', 'B', 'b']

C ['c', 'C']

A ['A', 'A', 'a']

 

# ######## imap() ########

>>> for x in itertools.imap(lambda x, y: x * y, [10, 20, 30], itertools.count(1)):

...     print x

 

itertools

 

閱讀: 5044

Python的內建模塊itertools提供了非常有用的用於操作迭代對象的函數。

 

首先,我們看看itertools提供的幾個“無限”迭代器:

 

>>> import itertools

>>> natuals = itertools.count(1)

>>> for n in natuals:

...     print n

...

1

2

3

...

因為count()會創建一個無限的迭代器,所以上述代碼會打印出自然數序列,根本停不下來,只能按Ctrl+C退出。

 

cycle()會把傳入的一個序列無限重復下去:

 

>>> import itertools

>>> cs = itertools.cycle('ABC') # 注意字符串也是序列的一種

>>> for c in cs:

...     print c

...

'A'

'B'

'C'

'A'

'B'

'C'

...

同樣停不下來。

 

repeat()負責把一個元素無限重復下去,不過如果提供第二個參數就可以限定重復次數:

 

>>> ns = itertools.repeat('A', 10)

>>> for n in ns:

...     print n

...

打印10次'A'

無限序列只有在for迭代時才會無限地迭代下去,如果只是創建了一個迭代對象,它不會事先把無限個元素生成出來,事實上也不可能在內存中創建無限多個元素。

 

無限序列雖然可以無限迭代下去,但是通常我們會通過takewhile()等函數根據條件判斷來截取出一個有限的序列:

 

>>> natuals = itertools.count(1)

>>> ns = itertools.takewhile(lambda x: x <= 10, natuals)

>>> for n in ns:

...     print n

...

打印出1到10

itertools提供的幾個迭代器操作函數更加有用:

 

chain()

 

chain()可以把一組迭代對象串聯起來,形成一個更大的迭代器:

 

for c in itertools.chain('ABC', 'XYZ'):

    print c

# 迭代效果:'A' 'B' 'C' 'X' 'Y' 'Z'

groupby()

 

groupby()把迭代器中相鄰的重復元素挑出來放在一起:

 

>>> for key, group in itertools.groupby('AAABBBCCAAA'):

...     print key, list(group) # 為什么這里要用list()函數呢?

...

A ['A', 'A', 'A']

B ['B', 'B', 'B']

C ['C', 'C']

A ['A', 'A', 'A']

實際上挑選規則是通過函數完成的,只要作用於函數的兩個元素返回的值相等,這兩個元素就被認為是在一組的,而函數返回值作為組的key。如果我們要忽略大小寫分組,就可以讓元素'A''a'都返回相同的key:

 

>>> for key, group in itertools.groupby('AaaBBbcCAAa', lambda c: c.upper()):

...     print key, list(group)

...

A ['A', 'a', 'a']

B ['B', 'B', 'b']

C ['c', 'C']

A ['A', 'A', 'a']

imap()

 

imap()和map()的區別在於,imap()可以作用於無窮序列,並且,如果兩個序列的長度不一致,以短的那個為准。

 

>>> for x in itertools.imap(lambda x, y: x * y, [10, 20, 30], itertools.count(1)):

...     print x

 

注意imap()返回一個迭代對象,而map()返回list。當你調用map()時,已經計算完畢:

 

>>> r = map(lambda x: x*x, [1, 2, 3])

>>> r # r已經計算出來了

[1, 4, 9]

當你調用imap()時,並沒有進行任何計算:

 

>>> r = itertools.imap(lambda x: x*x, [1, 2, 3])

>>> r

<itertools.imap object at 0x103d3ff90>

# r只是一個迭代對象

 

itertools

 

閱讀: 5044

Python的內建模塊itertools提供了非常有用的用於操作迭代對象的函數。

 

首先,我們看看itertools提供的幾個“無限”迭代器:

 

>>> import itertools

>>> natuals = itertools.count(1)

>>> for n in natuals:

...     print n

...

1

2

3

...

因為count()會創建一個無限的迭代器,所以上述代碼會打印出自然數序列,根本停不下來,只能按Ctrl+C退出。

 

cycle()會把傳入的一個序列無限重復下去:

 

>>> import itertools

>>> cs = itertools.cycle('ABC') # 注意字符串也是序列的一種

>>> for c in cs:

...     print c

...

'A'

'B'

'C'

'A'

'B'

'C'

...

同樣停不下來。

 

repeat()負責把一個元素無限重復下去,不過如果提供第二個參數就可以限定重復次數:

 

>>> ns = itertools.repeat('A', 10)

>>> for n in ns:

...     print n

...

打印10次'A'

無限序列只有在for迭代時才會無限地迭代下去,如果只是創建了一個迭代對象,它不會事先把無限個元素生成出來,事實上也不可能在內存中創建無限多個元素。

 

無限序列雖然可以無限迭代下去,但是通常我們會通過takewhile()等函數根據條件判斷來截取出一個有限的序列:

 

>>> natuals = itertools.count(1)

>>> ns = itertools.takewhile(lambda x: x <= 10, natuals)

>>> for n in ns:

...     print n

...

打印出1到10

itertools提供的幾個迭代器操作函數更加有用:

 

chain()

 

chain()可以把一組迭代對象串聯起來,形成一個更大的迭代器:

 

for c in itertools.chain('ABC', 'XYZ'):

    print c

# 迭代效果:'A' 'B' 'C' 'X' 'Y' 'Z'

groupby()

 

groupby()把迭代器中相鄰的重復元素挑出來放在一起:

 

>>> for key, group in itertools.groupby('AAABBBCCAAA'):

...     print key, list(group) # 為什么這里要用list()函數呢?

...

A ['A', 'A', 'A']

B ['B', 'B', 'B']

C ['C', 'C']

A ['A', 'A', 'A']

實際上挑選規則是通過函數完成的,只要作用於函數的兩個元素返回的值相等,這兩個元素就被認為是在一組的,而函數返回值作為組的key。如果我們要忽略大小寫分組,就可以讓元素'A''a'都返回相同的key:

 

>>> for key, group in itertools.groupby('AaaBBbcCAAa', lambda c: c.upper()):

...     print key, list(group)

...

A ['A', 'a', 'a']

B ['B', 'B', 'b']

C ['c', 'C']

A ['A', 'A', 'a']

imap()

 

imap()和map()的區別在於,imap()可以作用於無窮序列,並且,如果兩個序列的長度不一致,以短的那個為准。

 

>>> for x in itertools.imap(lambda x, y: x * y, [10, 20, 30], itertools.count(1)):

...     print x

...

10

40

90

注意imap()返回一個迭代對象,而map()返回list。當你調用map()時,已經計算完畢:

 

>>> r = map(lambda x: x*x, [1, 2, 3])

>>> r # r已經計算出來了

[1, 4, 9]

當你調用imap()時,並沒有進行任何計算:

 

>>> r = itertools.imap(lambda x: x*x, [1, 2, 3])

>>> r

<itertools.imap object at 0x103d3ff90>

# r只是一個迭代對象

必須用for循環對r進行迭代,才會在每次循環過程中計算出下一個元素:

 

>>> for x in r:

...     print x

...

1

4

9

這說明imap()實現了“惰性計算”,也就是在需要獲得結果的時候才計算。類似imap()這樣能夠實現惰性計算的函數就可以處理無限序列:

 

>>> r = itertools.imap(lambda x: x*x, itertools.count(1))

>>> for n in itertools.takewhile(lambda x: x<100, r):

...     print n
View Code

 

 

17、logging模塊

用於便捷記錄日志且線程安全的模塊

import logging

logging.basicConfig(filename='log.log',

                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',

                    datefmt='%Y-%m-%d %H:%M:%S %p',

                    level=5)

 

logging.debug('debug')

logging.info('info')

logging.warning('warning')

logging.error('error')

logging.critical('critical')

logging.log(10,'log')

 

對於等級:

CRITICAL = 50

FATAL = CRITICAL

ERROR = 40

WARNING = 30

WARN = WARNING

INFO = 20

DEBUG = 10

NOTSET = 0
View Code

 

 

 

 出處:http://blog.51cto.com/yht1990/1845737


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM