Python官方教程的最後一個部分就是标準庫概覽,在這裡我們浏覽一下标準庫,了解一下Python标準庫包含了哪些功能。
作業系統和檔案操作
os
os子產品包含了目前作業系統的抽象,我們可以利用os子產品對作業系統進行各種通路。下面使用os子產品的幾個方法和屬性,通路了目前腳本路徑、作業系統名以及整個環境變量。
print('--------------os--------------')
import os
print(f'current dir:{os.curdir}')
print(f'os name:{os.name}')
print(f'os path:{os.environ}')
print(f'os linesep:{os.linesep}')
shutil
該子產品包含了檔案和檔案夾的通用工具、包括移動、複制檔案和檔案夾等等。
print('--------------shutil--------------')
import shutil
hosts_file = r'C:\Windows\System32\drivers\etc\hosts'
dest_file = r'D:\Desktop\hosts.txt'
shutil.copy2(hosts_file, dest_file)
glob
glob子產品提供了通配符來選擇檔案。
print('--------------glob--------------')
import glob
source_files = glob.glob('*.py')
print(source_files)
sys
sys子產品的
argv
屬性可以擷取目前Python腳本執行時的指令行參數。
print('--------------sys--------------')
import sys
print(sys.argv)
sys子產品還有幾個屬性,用于向标準輸入、輸出、錯誤流寫入和讀取資料。例如下面的例子将向标準錯誤流輸出了一些資訊。
sys.stderr.write('This is a error\n')
正規表達式
re子產品用于處理正規表達式。
下面的例子查找所有以
f
開頭的單詞。
print('--------------re--------------')
import re
long_sentence = '''\
When symlinks is false, if the file pointed by the symlink doesn’t exist, an exception will be added in the list of errors raised in an Error exception at the end of the copy process. You can set the optional ignore_dangling_symlinks flag to true if you want to silence this exception. Notice that this option has no effect on platforms that don’t support os.symlink().
'''
results = re.findall(r'\bf\w+', long_sentence)
print(results)
數學計算
math
math子產品包含了很多數學計算的函數。如果需要進階數學計算,可以查閱
awesome-python
項目查找流行的數學計算子產品。
print('--------------math--------------')
import math
print(f'PI is {math.pi}')
print(f'e is {math.e}')
random
random子產品包含了一些生成随機數的函數。
print('--------------random--------------')
import random
for i in range(1, 6):
print(random.choice([1, 2, 3, 4, 5]), end=' ')
print()
for i in range(1, 6):
print(random.randrange(1, 100), end=' ')
print()
for i in range(1, 6):
print(random.randint(1, 10), end=' ')
print()
for i in range(1, 6):
print(random.uniform(1, 10), end=' ')
print()
list1 = [1, 2, 3, 4, 5]
random.shuffle(list1)
print(f'打亂之後:{list1}')
statistics
statistics子產品可用于基本的統計。
print('--------------statistics--------------')
import statistics
data = [1, 2, 3, 4, 5, 6, 7, 4, 5, 6, 2, 3, 4, 4, 4, 4]
print(f'平均數:{statistics.mean(data)}')
print(f'中位數:{statistics.median(data)}')
print(f'方差:{statistics.variance(data)}')
print(f'标準差:{statistics.stdev(data)}')
print(f'衆數:{statistics.mode(data)}')
網絡
urllib.request
和
urllib.smtp
是處理網絡的兩個包,用于發起網絡請求以及收發電子郵件。
print('--------------urllib.request--------------')
import urllib.request
with urllib.request.urlopen('http://www.baidu.com') as web:
for line in web:
print(line.decode('UTF8'),end='')
日期時間
datetime子產品包含了日期時間的處理。
print('--------------datetime--------------')
import datetime
today = datetime.date.today()
now = datetime.datetime.today()
print(f'today:{today}')
print(f'now:{now}')
my_age = today - datetime.date(1994, 7, 7)
print(f'my age:{my_age.days/365}')
資料壓縮
zlib子產品可用于資料壓縮。
print('--------------zlib--------------')
import zlib
data = b'aaaaa bbbbbbb cccccc dddddddd'
compressed = zlib.compress(data)
print(f'data length:{len(data)}, compressed length:{len(compressed)}')
print(f'compressed:{str(compressed)}')
data = zlib.decompress(compressed)
print(f'data:{str(data)}')
其他子產品
标準庫的子產品有很多,這裡不介紹了。有興趣的請直接檢視相應資料。
timeit
、
profile
pstats
子產品可用于性能測量。
doctest
unittest
用于進行測試。
json
xml
csv
等子產品可以處理相應資料。
sqlite3
子產品用于處理Sqlite3嵌入式資料庫。
gettext
locale
codecs
等子產品用于國際化。