Python标准库详细介绍与基本使用方式,超详细!

趁着刚吃完饭偷个闲,写一篇关于Python标准库的详细操作,很简单的!

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

目录:

在开始之前先给大家点福利,小编准备了Python入门的系统教程等资料,进群:943752371就可以自动领取了!

这是我的微信公众号【Python编程之家】各位大佬用空可以关注下,每天更新Python学习方法,感谢!

《Python标准库详细介绍与基本使用方式,超详细!》 111111111111.png

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

Python 标准库概览概览

操作系统接口

os 模块提供了很多与操作系统交互的函数:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

应该用 import os 风格而非 from os import *。这样可以保证随操作系统不同而有所变化的 os.open() 不会覆盖内置函数 open()。

在使用一些像 os 这样的大型模块时内置的 dir() 和 help() 函数非常有用:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

针对日常的文件和目录管理任务,shutil 模块提供了一个易于使用的高级接口:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

glob 模块提供了一个函数用于从目录通配符搜索中生成文件列表:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>>>> import glob

glob.glob(‘*.py’)
[‘primes.py’, ‘random.py’, ‘quote.py’]
</pre>

命令行参数 x

通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量。例如在命令行中执行 python demo.py one two three 后可以得到以下输出结果:

getopt 模块使用 Unix getopt() 函数处理 sys.argv。更多的复杂命令行处理由 argparse 模块提供。

错误输出重定向和程序终止

sys 还有 stdin, stdout 和 stderr 属性,即使在 stdout 被重定向时,后者也可以用于显示警告和错误信息:

sys.stderr.write(‘Warning, log file not found starting a new one ‘)

Warning, log file not found starting a new one12

大多脚本的直接终止都使用 sys.exit()。

字符串正则匹配

re 模块为高级字符串处理提供了正则表达式工具。对于复杂的匹配和处理,正则表达式提供了简洁、优化的解决方案:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

只需简单的操作时,字符串方法最好用,因为它们易读,又容易调试:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>>>> ‘tea for too’.replace(‘too’, ‘two’)
‘tea for two’
</pre>

数学

math 模块为浮点运算提供了对底层C函数库的访问:

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>>>> import math

math.cos(math.pi / 4.0)
0.70710678118654757
math.log(1024, 2)
10.0
</pre>

random 提供了生成随机数的工具:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

互联网访问

有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个是用于处理从 urls 接收的数据的 urllib.request 以及用于发送电子邮件的 smtplib:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

(注意第二个例子需要在 localhost 运行一个邮件服务器。)

日期和时间

datetime 模块为日期和时间处理同时提供了简单和复杂的方法。支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出。该模块还支持时区处理。

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

数据压缩

以下模块直接支持通用的数据打包和压缩格式:zlib, gzip, bz2, lzma, zipfile 以及 tarfile。

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

性能度量

有些用户对了解解决同一问题的不同方法之间的性能差异很感兴趣。Python 提供了一个度量工具,为这些问题提供了直接答案。

例如,使用元组封装和拆封来交换元素看起来要比使用传统的方法要诱人的多。timeit 证明了后者更快一些:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

相对于 timeit 的细粒度,profile 和 pstats 模块提供了针对更大代码块的时间度量工具。

质量控制

开发高质量软件的方法之一是为每一个函数开发测试代码,并且在开发过程中经常进行测试。

doctest 模块提供了一个工具,扫描模块并根据程序中内嵌的文档字符串执行测试。测试构造如同简单的将它的输出结果剪切并粘贴到文档字符串中。通过用户提供的例子,它发展了文档,允许 doctest 模块确认代码的结果是否与文档一致:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

unittest 模块不像 doctest 模块那么容易使用,不过它可以在一个独立的文件里提供一个更全面的测试集:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

“瑞士军刀”

Python 展现了“瑞士军刀”的哲学。这可以通过它更大的包的高级和健壮的功能来得到最好的展现。列如:

《Python标准库详细介绍与基本使用方式,超详细!》 Python标准库详细介绍与基本使用方式,超详细!

好了,小编今天就分享到这,小伙伴们赶紧 去自己敲代码试试吧!Python最新的系统学习资料可以私信小编回复我要资料就可以自动领取了!

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/9ec06d39241f
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞