Python 标准库一览

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.requesturllib.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)}')

其他模块

标准库的模块有很多,这里不介绍了。有兴趣的请直接查看相应资料。

timeitprofilepstats模块可用于性能测量。

doctestunittest用于进行测试。

jsonxmlcsv等模块可以处理相应数据。

sqlite3模块用于处理Sqlite3嵌入式数据库。

gettextlocalecodecs等模块用于国际化。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • GitHub 上有一个 Awesome - XXX 系列的资源整理,资源非常丰富,涉及面非常广。awesome-p...
    若与阅读 18,752评论 4 418
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,568评论 6 427
  • 环境管理管理Python版本和环境的工具。p–非常简单的交互式python版本管理工具。pyenv–简单的Pyth...
    MrHamster阅读 3,850评论 1 61
  • http://python.jobbole.com/85231/ 关于专业技能写完项目接着写写一名3年工作经验的J...
    燕京博士阅读 7,629评论 1 118
  • 父亲一词,只是一称呼代词,却又不止是一个称呼。当然,说的有点生硬的意味在里面,只是心中忽然间有感而发罢了。 ...
    我叫王杰阅读 489评论 0 1