Python 标准库一览
来源:互联网 发布:淘宝网结婚用品 编辑:程序博客网 时间:2024/06/08 04:27
Python官方教程的最后一个部分就是标准库概览,在这里我们浏览一下标准库,了解一下Python标准库包含了哪些功能。
操作系统和文件操作
os
os模块包含了当前操作系统的抽象,我们可以利用os模块对操作系统进行各种访问。下面使用os模块的几个方法和属性,访问了当前脚本路径、操作系统名以及整个环境变量。
print('--------------os--------------')import osprint(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 shutilhosts_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 globsource_files = glob.glob('*.py')print(source_files)
sys
sys模块的argv
属性可以获取当前Python脚本执行时的命令行参数。
print('--------------sys--------------')import sysprint(sys.argv)
sys模块还有几个属性,用于向标准输入、输出、错误流写入和读取数据。例如下面的例子将向标准错误流输出了一些信息。
sys.stderr.write('This is a error\n')
正则表达式
re模块用于处理正则表达式。
下面的例子查找所有以f
开头的单词。
print('--------------re--------------')import relong_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 mathprint(f'PI is {math.pi}')print(f'e is {math.e}')
random
random模块包含了一些生成随机数的函数。
print('--------------random--------------')import randomfor 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 statisticsdata = [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.requestwith urllib.request.urlopen('http://www.baidu.com') as web: for line in web: print(line.decode('UTF8'),end='')
日期时间
datetime模块包含了日期时间的处理。
print('--------------datetime--------------')import datetimetoday = 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 zlibdata = 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
等模块用于国际化。
0 0
- Python 标准库一览
- Python 标准库一览(Python进阶学习)
- Python 标准库一览(Python进阶学习)
- Ruby标准库一览
- C++0x标准库改动一览
- C++ 常用标准库头文件一览
- ruby 标准库一览(备注)
- 电平标准一览
- python -- Python 标准库
- 各种平台和标准库定义的数据类型一览
- python 标准库库
- Python标准库
- python标准库
- Python标准库
- python 标准库
- python学习标准库
- Python标准库简介
- python学习标准库
- iOS 在Unit Testing 里面使用第三方库
- 【Eigen】How to "install" Eigen?
- BestCoder Round #93酱油记
- 389. Find the Difference Add to List(新添字符)
- 在NotePad++中直接运行TCL
- Python 标准库一览
- vb.net 教程 5-12 绘图实例之统计图 2 折线图
- Linux 下搭建 chrome+postman环境
- 3223. HEOI2013 Ede的新背包问题
- 浮点型数据在计算机内存中存储原理
- 4月1日,StringEqualTest,每日20行。
- Java中的LinkedList
- 历届试题 邮局
- Mac 下Android Studio配置Gradle版本