博客
关于我
第十章 标准库简介——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/

你可能感兴趣的文章
NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
查看>>
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>