文章出處

Python自動化 【第五篇】:Python基礎-常用模塊

目錄

 

  1. 模塊介紹
  2. time和datetime模塊
  3. random
  4. os
  5. sys
  6. shutil
  7. json和pickle
  8. shelve
  9. xml處理
  10. yaml處理
  11. configparser
  12. hashlib
  13. re正則表達式

 

1.      模塊介紹

1.1    定義 

能夠實現某個功能的代碼集合(本質是py文件)  test.p的模塊名是test包的定義:用來從邏輯上組織模塊,本質就是一個目錄(必須帶有一個__init__.py文件)

1.2    導入方法

  a) Import module

  b) Import module1,module2

  c) From module import *

  d) From module import m1,m2,m3

  e) From module import logger as module_logger

1.3    Import 本質

  導入模塊的本質就是把python文件解釋一遍

  導入包的本質就是在執行該包下的__init__.py文件

1.4    導入優化

  From module import test as module_test

1.5    模塊的分類

  a) 標準庫

  b) 開源模塊(第三方模塊)

  c) 自定義模塊

 

2.      time & datetime 模塊

time的三種表現方式:

  1)時間戳(用秒來表示)

  2)格式化的時間字符串

  3)元組(struct_time)共九個元素。

2.1    時間戳

 1 1 import time
 2 
 3 2 # print(time.clock()) #返回處理器時間,3.3開始已廢棄 , 改成了time.process_time()測量處理器運算時間,不包括sleep時間,不穩定,mac上測不出來
 4 
 5 3 # print(time.altzone)  #返回與utc時間的時間差,以秒計算\
 6 
 7 4 # print(time.asctime()) #返回時間格式"Fri Aug 19 11:14:16 2016",
 8 
 9 5 # print(time.localtime()) #返回本地時間 的struct time對象格式
10 
11 6 # print(time.gmtime(time.time()-800000)) #返回utc時間的struc時間對象格式
12 
13 7
14 
15 8 # print(time.asctime(time.localtime())) #返回時間格式"Fri Aug 19 11:14:16 2016",
16 
17 9 #print(time.ctime()) #返回Fri Aug 19 12:38:29 2016 格式, 同上
18 
19 10
20 
21 11 # 日期字符串 轉成  時間戳
22 
23 12 # string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #將 日期字符串 轉成 struct時間對象格式
24 
25 13 # print(string_2_struct)
26 
27 14 # struct_2_stamp = time.mktime(string_2_struct) #將struct時間對象轉成時間戳
28 
29 15 # print(struct_2_stamp)
30 
31 16 #將時間戳轉為字符串格式
32 
33 17 # print(time.gmtime(time.time()-86640)) #將utc時間戳轉換成struct_time格式
34 
35 18 # print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #將utc struct_time格式轉成指定的字符串格式
36 
37 19 #時間加減
38 
39 20 import datetime
40 
41 21 # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
42 
43 22 #print(datetime.date.fromtimestamp(time.time()) )  # 時間戳直接轉成日期格式 2016-08-19
44 
45 23 # print(datetime.datetime.now() )
46 
47 24 # print(datetime.datetime.now() + datetime.timedelta(3)) #當前時間+3天
48 
49 25 # print(datetime.datetime.now() + datetime.timedelta(-3)) #當前時間-3天
50 
51 26 # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #當前時間+3小時
52 
53 27 # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #當前時間+30分
54 
55 28 # c_time  = datetime.datetime.now()
56 
57 29 # print(c_time.replace(minute=3,hour=2)) #時間替換
View Code 

2.2    格式化的時間字符串 

格式參照:

  %a    本地(locale)簡化星期名稱

  %A    本地完整星期名稱

  %b    本地簡化月份名稱

  %B    本地完整月份名稱

  %c    本地相應的日期和時間表示

  %d    一個月中的第幾天(01 - 31)

  %H    一天中的第幾個小時(24小時制,00 - 23)

  %I    第幾個小時(12小時制,01 - 12)

  %j    一年中的第幾天(001 - 366)

  %m    月份(01 - 12)

  %M    分鐘數(00 - 59)

  %p    本地am或者pm的相應符    一

  %S    秒(01 - 61)    二

  %U    一年中的星期數。(00 - 53星期天是一個星期的開始。)第一個星期天之前的所有天數都放在第0周。    三

  %w    一個星期中的第幾天(0 - 6,0是星期天)    三

  %W    和%U基本相同,不同的是%W以星期一為一個星期的開始。

  %x    本地相應日期

  %X    本地相應時間

  %y    去掉世紀的年份(00 - 99)

  %Y    完整的年份

  %Z    時區的名字(如果不存在為空字符)

  %%    ‘%’字符

2.3    時間關系轉換

  

3.     random模塊

3.1    隨機數

import random

print (random.random())  #0.6445010863311293  

#random.random()用于生成一個0到1的隨機符點數: 0 <= n < 1.0

print (random.randint(1,7)) #4

#random.randint()的函數原型為:random.randint(a, b),用于生成一個指定范圍內的整數。

# 其中參數a是下限,參數b是上限,生成的隨機數n: a <= n <= b

print (random.randrange(1,10)) #5

#random.randrange的函數原型為:random.randrange([start], stop[, step]),

# 從指定范圍內,按指定基數遞增的集合中 獲取一個隨機數。如:random.randrange(10, 100, 2),

# 結果相當于從[10, 12, 14, 16, ... 96, 98]序列中獲取一個隨機數。

# random.randrange(10, 100, 2)在結果上與 random.choice(range(10, 100, 2) 等效。

print(random.choice('liukuni')) #i

#random.choice從序列中獲取一個隨機元素。

# 其函數原型為:random.choice(sequence)。參數sequence表示一個有序類型。

# 這里要說明一下:sequence在python不是一種特定的類型,而是泛指一系列的類型。

# list, tuple, 字符串都屬于sequence。有關sequence可以查看python手冊數據模型這一章。

# 下面是使用choice的一些例子:

print(random.choice("學習Python"))#學

print(random.choice(["JGood","is","a","handsome","boy"]))  #List

print(random.choice(("Tuple","List","Dict")))   #List

print(random.sample([1,2,3,4,5],3))    #[1, 2, 5]

#random.sample的函數原型為:random.sample(sequence, k),從指定序列中隨機獲取指定長度的片斷。sample函數不會修改原有序列。
View Code

3.2    實際應用

#!/usr/bin/env python

# encoding: utf-8

import random

import string



# 隨機整數:

print(random.randint(0, 99))  # 70



# 隨機選取0到100間的偶數:

print(random.randrange(0, 101, 2))  # 4



# 隨機浮點數:

print(random.random())  # 0.2746445568079129

print(random.uniform(1, 10))  # 9.887001463194844



# 隨機字符:

print(random.choice('abcdefg&#%^*f'))  # f



# 多個字符中選取特定數量的字符:

print(random.sample('abcdefghij', 3))  # ['f', 'h', 'd']



# 隨機選取字符串:

print(random.choice(['apple', 'pear', 'peach', 'orange', 'lemon']))  # apple

# 洗牌#

items = [1, 2, 3, 4, 5, 6, 7]

print(items)  # [1, 2, 3, 4, 5, 6, 7]

random.shuffle(items)

print(items)  # [1, 4, 7, 2, 5, 3, 6]
View Code

3.3    生成隨機驗證碼

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

 

4.      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下為"\r\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所指向的文件或者目錄的最后修改時間

 

5.      sys模塊

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]

 

6.      shutil模塊

  
import shutil
f1 = open("file.txt", encoding="utf-8")
f2 = open("file2.txt", "w",encoding="utf-8")
shutil.copyfileobj(f1,f2)
View Code

 

shutil.copyfile() 輸入源文件就copy:

  
shutil.copyfile("file1", "file2")
View Code 

shutil.copymode() 僅拷貝權限,內容、組、用戶均不變(待實驗)

shutil.copystat() 拷貝權限,沒有創建新文件

shutil.copy() 拷貝文件

shutil.copy2() 所有都拷貝(文件和狀態信息) 

shutil.copytree() 遞歸拷貝文件(將文件和所在目錄都拷貝)

  
shutil.copytree("test1", "test2")
View Code 

shutil.rmtree() 遞歸刪除文件  比調用shell命令高效

  
shutil.rmtree("test3")
View Code

shutil.move() 遞歸的移動文件

shutil.make_archive(base_name, format, file) 

import shutil

shutil.make_archive("shutil_archive_test", "zip", "E:\Pycharm\day5")
View Code 

zipfile

import zipfile
z = zipfile.ZipFile("file1.zip", "w")  # 指定壓縮后的文件名是file1.txt
z.write("test1.py")  # 先把test1.py壓縮至file1.zip
print("----------")  # 可以干些其他事
z.write("test2.py")  # 然后把test2.py壓縮至file1.zip
z.close()
View Code

 

7.      jsonpickle模塊      

解決了不同語言不同平臺的之間的數據交換

參考:http://www.cnblogs.com/ZhPythonAuto/p/5786091.html

 

8.      shelve模塊

shelve模塊是一個簡單的k,v將內存數據通過文件持久化的模塊,可以持久化任何pickle可支持的python數據格式。

import shelve
import datetime
d = shelve.open('shelve_test')  # 打開一個文件

# info = {"age":22,"job":"it"}
#
# name = ["alex", "rain", "test"]
# d["name"] = name  # 持久化列表
# d["info"] = info  # 持久化類
# d["date"] =datetime.datetime.now()
# d.close()
print(d.get("name"))
print(d.get("info"))
print(d.get("date"))
View Code

 

9.      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

 

xml協議在各個語言里的都是支持的,在python中可以用以下模塊操作xml

import xml.etree.ElementTree as ET



tree = ET.parse("xmltest.xml")

root = tree.getroot()

print(root.tag)



# 遍歷xml文檔

for child in root:

    print(child.tag, child.attrib)

    for i in child:

        print(i.tag, i.text, i.attrib)



# 只遍歷year 節點

for node in root.iter('year'):

    print(node.tag, node.text)
修改和刪除xml文檔內import xml.etree.ElementTree as ET


tree = ET.parse("xmltest.xml")

root = tree.getroot()



# 修改

for node in root.iter('year'):

    new_year = int(node.text) + 1

    node.text = str(new_year)

    node.set("updated", "yes")



tree.write("xmltest.xml")



# 刪除node

for country in root.findall('country'):

    rank = int(country.find('rank').text)

    if rank > 50:

        root.remove(country)


tree.write('output.xml')
View Code

 

自己創建xml文檔

import xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
age = ET.SubElement(name, "age", attrib={"checked": "no"})
sex = ET.SubElement(name, "sex")
age.text = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19'

et = ET.ElementTree(new_xml)  # 生成文檔對象
et.write("test.xml", encoding="utf-8", xml_declaration=True)

ET.dump(new_xml)  # 打印生成的格式
View Code

 

10.     PyYAML模塊

   yaml語法(用作配置文件)

   數據結構可以用類似大綱的縮排方式呈現,結構通過縮進來表示,連續的項目通過減號“-”來表示,map結構里面的key/value對用冒號“:”來分隔。樣例如下:

house:
  family:
    name: Doe
    parents:
      - John
      - Jane
    children:
      - Paul
      - Mark
      - Simone
  address:
    number: 34
    street: Main Street
    city: Nowheretown
    zipcode: 12345
View Code

 

11.      ComfigParser模塊

用于生成和修改常見配置文檔,當前模塊的名稱在 python 3.x 版本中變更為 configparser。

格式如下:

[DEFAULT]

ServerAliveInterval = 45

Compression = yes

CompressionLevel = 9

ForwardX11 = yes



[bitbucket.org]

User = hg



[topsecret.server.com]

Port = 50022

ForwardX11 = no
View Code

 

用python生成一個這樣的文檔

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)
View Code

 

寫完后還可以讀出來:

>>> import configparser

>>> config = configparser.ConfigParser()

>>> config.sections()

[]

>>> config.read('example.ini')

['example.ini']

>>> config.sections()

['bitbucket.org', 'topsecret.server.com']

>>> 'bitbucket.org' in config

True

>>> 'bytebong.com' in config

False

>>> config['bitbucket.org']['User']

'hg'

>>> config['DEFAULT']['Compression']

'yes'

>>> topsecret = config['topsecret.server.com']

>>> topsecret['ForwardX11']

'no'

>>> topsecret['Port']

'50022'

>>> for key in config['bitbucket.org']: print(key)

...

user

compressionlevel

serveraliveinterval

compression

forwardx11

>>> config['bitbucket.org']['ForwardX11']

'yes'
View Code

 

configparser增刪改查語法

[section1]
k1 = v1
k2:v2

[section2]
k1 = v1

import ConfigParser

config = ConfigParser.ConfigParser()
config.read('i.cfg')

# ########## 讀 ##########
# secs = config.sections()
# print secs
# options = config.options('group2')
# print options

# item_list = config.items('group2')
# print item_list

# val = config.get('group1','key')
# val = config.getint('group1','key')

# ########## 改寫 ##########
# sec = config.remove_section('group1')
# config.write(open('i.cfg', "w"))

# sec = config.has_section('wupeiqi')
# sec = config.add_section('wupeiqi')
# config.write(open('i.cfg', "w"))


# config.set('group2','k1',11111)
# config.write(open('i.cfg', "w"))

# config.remove_option('group2','age')
# config.write(open('i.cfg', "w"))
View Code

 

12.      hashlib模塊

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

import hashlib



m = hashlib.md5()

m.update(b"Hello")

m.update(b"It's me")

print(m.digest())

m.update(b"It's been a long time since last time we ...")



print(m.digest())  # 2進制格式hash

print(len(m.hexdigest()))  # 16進制格式hash

'''

def digest(self, *args, **kwargs): # real signature unknown

    """ Return the digest value as a string of binary data. """

    pass



def hexdigest(self, *args, **kwargs): # real signature unknown

    """ Return the digest value as a string of hexadecimal digits. """

    pass



'''

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())
View Code

 

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

import hmac
h = hmac.new('wueiqi')
h.update('hellowo')
print h.hexdigest()
View Code

 

13.      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      匹配字符并替換

 

幾個匹配模式

  re.I(re.IGNORECASE): 忽略大小寫(括號內是完整寫法,下同)

  M(MULTILINE): 多行模式,改變'^'和'$'的行為

  S(DOTALL): 點任意匹配模式,改變'.'的行為


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()