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

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

Python ?????

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

??????

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

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

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

?????

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

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

?????

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

import argparse
parser = 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 re
re.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 math
math.cos(math.pi / 4) # 0.70710678118654757
import statistics
data = [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 date
now = date.today()
now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.") # ????????

????

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

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

????

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

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

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

????

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

import doctest
doctest.testmod() # ????????
import unittest
class 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/

你可能感兴趣的文章
Node第一天
查看>>
node编译程序内存溢出
查看>>
Node读取并输出txt文件内容
查看>>
node防xss攻击插件
查看>>
noi 1996 登山
查看>>
noi 7827 质数的和与积
查看>>
NOI-1.3-11-计算浮点数相除的余数
查看>>
noi.ac #36 模拟
查看>>
NOI2010 海拔(平面图最大流)
查看>>
NOIp2005 过河
查看>>
NOIP2011T1 数字反转
查看>>
NOIP2014 提高组 Day2——寻找道路
查看>>
noip借教室 题解
查看>>
NOIP模拟测试19
查看>>
NOIp模拟赛二十九
查看>>
Vue3+element plus+sortablejs实现table列表拖拽
查看>>
Nokia5233手机和我装的几个symbian V5手机软件
查看>>
non linear processor
查看>>
Non-final field ‘code‘ in enum StateEnum‘
查看>>
none 和 host 网络的适用场景 - 每天5分钟玩转 Docker 容器技术(31)
查看>>