Python 标准库一览

作者: 乐百川 | 来源:发表于2017-04-01 22:34 被阅读116次

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等模块用于国际化。

相关文章

  • Python 标准库一览

    Python官方教程的最后一个部分就是标准库概览,在这里我们浏览一下标准库,了解一下Python标准库包含了哪些功...

  • (三)python常用标准库

    python常用标准库 python标准库常见模块 操作系统相关:os 时间与日期:time、datetime 科...

  • python3从零学习-5.0、标准库

    Python 标准库 Python 标准库非常庞大,所提供的组件涉及范围十分广泛。这个库包含了多个内置模块 (...

  • Udacity Python 随笔 *

    Udacity Python入门 标准库推荐 Python 标准库的模块很多!为了帮助大家熟悉可用模块,以下是精选...

  • 24.python3标准库

    标准库概览 python3标准库官方文档:https://docs.python.org/zh-cn/3.7/li...

  • 标准库

    标准库 Python标准库中包含了大量有用的模块,同时也是每个标准的Python安装包中的一部分。熟悉Python...

  • Python 标准库

    很好的python标准库资源网站https://pymotw.com/3/ Python标准库 是一组模块, 安装...

  • Python

    教程类 Python 教程 官方教程Python 标准库ctypes --- Python 的外部函数库pyt...

  • python高级编程3

    1.模块进阶 Python有一套很有用的标准库(standard library)。标准库会随着Python解释器...

  • python爬虫脚本下载视频,同时借助FFmpeg合并视频

    requests是简洁的Python http库, 相较Python标准库urllib, requests更加人性...

网友评论

    本文标题:Python 标准库一览

    本文链接:https://www.haomeiwen.com/subject/oghwottx.html