Python常用标准库(batteries included)
来源:互联网 发布:unity3d 绳子插件 编辑:程序博客网 时间:2024/06/05 22:38
常用标准库:os, sys, random, re, shelve, collections, time, datetime, timeit, logging, json, pickle, request
sys模块
sys模块能够访问与Python解释器紧密联系的变量和函数,下面是 sys 模块中一些常用的函数和变量:
argv 命令行参数,包括脚本名称exit([arg]) 退出当前程序,可选参数给定的返回值或错误信息modules 隐射模块名字到载入模块的字典path 查看模块所在目录的目录名列表platform 类似sunos5或win32平台标识符stdin 标准输入流——一个类文件(file-like)对象stdout 标准输出流——一个类文件对象stderr 标准错误流——一个类文件对象
os模块
os模块提供了很多访问操作系统服务的功能。下面是一些常用的函数和变量:
environ 会环境变量进行隐射system(command) 在子shell中执行操作系统命令sep 路径中的分隔符pathsep 分隔路径的分隔符linesep 行分隔符('\n','\r','\r\n')urandom(n) 返回n自己的加密强随机数
time 模块
time 模块所包含的函数能够实现以下功能:获取当前时间、操作时间和日期、从字符串读取时间以及格式化时间字符串。日期可以使用实数(从“新纪元”的1月1日0 点开始计算到现在的秒数,新纪元是一个与平台相关的年份,对于UNIX来说是1970年),或者是包含9个整数的元组,它们分别表示下面的含义:
(2008,1,21,12,2,56,0,21,0) # 年、月、日、时、分、秒、周、儒日历、夏令时
下面是 time 模块最常用的函数:
asctime([tuple]) 将时间元组转换为字符串localtime([secs]) 将秒数转换为日期元组,以本地时间为准mktime(tuple) 将时间元组转换为本地时间sleep(secs) 休眠 secs秒strptime(string[, format]) 将字符串解析为时间元组time() 当前时间(新纪元开始后的秒数,以UTC为准)
random 模块
random 模块包含返回随机数(伪随机数)的函数,下面是 random 模块的重要函数:
random() 返回 0 ≤ n < 1 之间的随机实数n,其中 0 < n ≤ 1getrandbits(n) 以长整型形式返回n个随机数uniform(a,b) 返回随机实数n,其中 a ≤ n < brandrange([start],stop,[step]) 返回range(start,stop,step)中的随机数choice(seq) 从序列seq中返回随意元素shuffle(seq[, random]) 原地指定序列seqsample(seq,n) 从序列seq中选择n个随机且独立的元素
shelve 模块
使用 shelve 模块提供了一个简单的文件存储方案。我们可以将一个对象持久化到文件中,如下:
import sys, shelvedef main(): data = shelve.open("D:\\data.dat") employee = {} employee['name'] = 'sunshine' employee['email'] = 'sunshine@gmail.com' pid = '1' try: data[pid] = employee finally: data.close()if __name__ == '__main__': main()
持久化之后,可以再次读取文件中的内容:
import sys, shelvedef main(): pid = '1' data = shelve.open("D:\\data.dat") print data[pid] # {'name': 'sunshine', 'email': 'sunshine@gmail.com'}if __name__ == '__main__': main()
阅读全文
0 0
- Python常用标准库(batteries included)
- Beginning Python - Chapter10 : Batteries included
- start haskell with batteries included
- nose1.3.7文档翻译--6.Batteries included: builtin nose plugins
- Python 常用标准库介绍
- Python常用标准库操作
- Python常用标准库 --- sys
- Python常用标准库 --- os
- Python常用标准库 --- glob
- Python常用标准库 --- math
- Python常用标准库 --- random
- Python常用标准库 --- platform
- Python常用标准库 --- json
- Python常用标准库 --- time
- Python常用标准库 --- datetime
- python常用 标准库介绍
- Python常用标准库使用
- Python 官方标准常用库
- 火星人计数法
- 归并排序
- Android之连接指定SSID的无线网络
- Output\TEST.sct(7): error: L6236E: No section matches selector
- Hibernate入门07_继承映射02_joined方式
- Python常用标准库(batteries included)
- pe逆向入门笔记一汇编指令学习及crackme初体验
- MySQL索引介绍
- 计蒜客初赛5
- Android启动过程深入解析
- centos7安装ssh服务
- 采药
- DB2 权限
- 在Oracle12c上添加了Scott用户,但是为什么再次进入的时候却显示invalid username/password;logon denied