博客
关于我
第十章 标准库简介——python导引编译之十一
阅读量:382 次
发布时间:2019-03-05

本文共 2737 字,大约阅读时间需要 9 分钟。

Python ?????

Python ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

??????

os ????????????????????????????????????????????????

import osos.getcwd()  # ????????os.chdir('/server/accesslogs')  # ??????os.system('mkdir today')  # ?? mkdir ??

??????? import os ??? from os import *??????? os.open() ?????? open()???????????????????????????? dir() ? help() ??????????

?????

glob ???????????????????????

import globglob.glob('*.py')['primes.py', 'random.py', 'quote.py']

?????

????????????????????sys ??? argv ?????????????argparse ???????????????????????

import argparseparser = argparse.ArgumentParser(prog='top', description='Show top lines from each file')parser.add_argument('filenames', nargs='+')parser.add_argument('-l', '--lines', type=int, default=10)args = parser.parse_args()print(args)

??? top.py --lines=5 alpha.txt beta.txt ??args.lines ????? 5?args.filenames ? ['alpha.txt', 'beta.txt']?

????????????

sys ?????? stdin, stdout, ? stderr ??????????? stderr ??????? stdout ???????????????????????? sys.exit() ?????

???????

re ??????????????????????????????

import rere.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')['foot', 'fell', 'fastest']re.sub(r'(\b[a-z]+) \1', r'\1', 'cat in the the hat') 'cat in the hat'

???????????????????????

??

math ?????????????????random ???????????statistics ????????????????????

import mathmath.cos(math.pi / 4)  # 0.70710678118654757import statisticsdata = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]statistics.mean(data)  # 1.6071428571428572

?????

datetime ???????????????????????? date.today() ??????????????

from datetime import datenow = date.today()now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")  # ????????

????

???????? zlib, gzip, bz2, lzma ??????????????

import zlibs = b'witch which has which witches wrist watch't = zlib.compress(s)zlib.decompress(t)  # ???????

????

timeit ??????????????

from timeit import TimerTimer('t=a; a=b; b=t', 'a=1; b=2').timeit()  # 0.57535828626024577

profile ? pstats ??????????????????

????

doctest ????????????????unittest ???????????????

import doctestdoctest.testmod()  # ????????import unittestclass TestStatisticalFunctions(unittest.TestCase):    def test_average(self):        self.assertEqual(average([20, 30, 70]), 40.0)        self.assertEqual(round(average([1, 5, 7]), 1), 4.3)        with self.assertRaises(ZeroDivisionError):            average([])        with self.assertRaises(TypeError):            average(20, 30, 70)unittest.main()

????

Python??????????????????????xmlrpc ? xmlrpc.server ???????????????????? XML ???email ???????? MIME ???????????json ? csv ??????????????sqlite3 ???? SQLite ???????????????????? gettext, locale, ? codecs ?????

转载地址:http://xyng.baihongyu.com/

你可能感兴趣的文章
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>
nginx代理地图服务--离线部署地图服务(地图数据篇.4)
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>
Nginx反向代理与正向代理配置
查看>>
Nginx反向代理是什么意思?如何配置Nginx反向代理?
查看>>
nginx反向代理解决跨域问题,使本地调试更方便
查看>>
nginx启动脚本
查看>>
Nginx在Windows下载安装启动与配置前后端请求代理
查看>>
Nginx多域名,多证书,多服务配置,实用版
查看>>
nginx开机启动脚本
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>