【python标准库有哪些】教程文章相关的互联网学习教程文章

Python : 标准库-时间和日期补充【代码】

常用时间处理方法 今天 today = datetime.date.today() 昨天 yesterday = today - datetime.timedelta(days=1) 上个月 last_month = today.month - 1 if today.month - 1 else 12 当前时间戳 time_stamp = time.time() 时间戳转datetime datetime.datetime.fromtimestamp(time_stamp) datetime转时间戳 int(time.mktime(today.timetuple())) datetime转字符串 today_str = today.strftime("%Y-%m-%d") 字符串转datetime today = da...

Python : 标准库-关于urlopen的补充

处理get请求,不传data,则为get请求 import urllib from urllib.request import urlopen from urllib.parse import urlencode url=‘http://www.xxx.com/login’ data={“username”:“admin”,“password”:123456} req_data=urlencode(data)#将字典类型的请求数据转变为url编码 res=urlopen(url+’?’+req_data)#通过urlopen方法访问拼接好的url res=res.read().decode()#read()方法是读取返回数据内容,decode是转换返回数据的...

Python : 标准库-操作系统接口

os模块提供了不少与操作系统相关联的函数。import os os.getcwd() # 返回当前的工作目录 ‘C:\Python34’os.chdir(’/server/accesslogs’) # 修改当前的工作目录 os.system(‘mkdir today’) # 执行系统命令 mkdir 0建议使用 “import os” 风格而非 “from os import *”。这样可以保证随操作系统不同而有所变化的 os.open() 不会覆盖内置函数 open()。 在使用 os 这样的大型模块时内置的 dir() 和 help() 函数非常有用...

Python : 标准库-命令行参数

通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量。例如在命令行中执行 “python demo.py one two three” 后可以得到以下输出结果:import sys print(sys.argv) [‘demo.py’, ‘one’, ‘two’, ‘three’]

Python : 标准库-字符串正则匹配

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’如果只需要简单的功能,应该首先考虑字符串方法,因为它们非常简单,易于阅读和调试:‘tea for too’.replace(‘too’, ...

Python : 标准库-数学

math模块为浮点运算提供了对底层C函数库的访问:import math math.cos(math.pi / 4) 0.70710678118654757math.log(1024, 2) 10.0

Python : 标准库-random

提供了生成随机数的工具。import random random.choice([‘apple’, ‘pear’, ‘banana’]) ‘apple’random.sample(range(100), 10) # sampling without replacement [30, 83, 16, 4, 8, 81, 41, 50, 18, 33]random.random() # random float 0.17970987693706186random.randrange(6) # random integer chosen from range(6) 4

Python : 标准库-访问互联网

有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个是用于处理从 urls 接收的数据的 urllib.request 以及用于发送电子邮件的 smtplib:from urllib.request import urlopen for line in urlopen(‘http://tycho.usno.navy.mil/cgi-bin/timer.pl’): … line = line.decode(‘utf-8’) # Decoding the binary data to text. … if ‘EST’ in line or ‘EDT’ in line: # look for Eastern Time … ...

Python : 标准库-日期和时间

datetime模块为日期和时间处理同时提供了简单和复杂的方法。 支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出。 该模块还支持时区处理:dates are easily constructed and formatted from datetime import date now = date.today() now datetime.date(2003, 12, 2)now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.") ‘12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.’dates su...

python – 标准库中定义的标准输入,输出和错误描述符常量是什么?【代码】

我在Python库中的任何地方都找不到这些基本常量.STDIN = 0 STDOUT = 1 STDERR = 2这是微不足道的,但我简直无法相信我必须自己定义它们.解决方法:可以从sys模块确定值:sys.stdout.fileno() sys.stdin.fileno() sys.stderr.fileno()

python标准库之shutil——可操作权限的文件操作库

转载自:https://www.jb51.net/article/145522.htm shutil模块提供了许多关于文件和文件集合的高级操作,特别提供了支持文件复制和删除的功能。 文件夹与文件操作 copyfileobj(fsrc, fdst, length=16*1024): 将fsrc文件内容复制至fdst文件,length为fsrc每次读取的长度,用做缓冲区大小fsrc: 源文件 fdst: 复制至fdst文件 length: 缓冲区大小,即fsrc每次读取的长度 ?1 2 3 4import shutil f1 = open("file.txt","r") f2 =...

python标准库之random模块【代码】【图】

Python中的random模块用于生成随机数。 下面具体介绍random模块的功能: 1.random.random() #用于生成一个0到1的 随机浮点数:0<= n < 1.01 import random 2 a = random.random() 3 print (a) 2.random.uniform(a,b) #用于生成一个指定范围内的随机符点数,两个参数其中一个是上限,一个是下限。如果a > b,则生成的随机数n: a <= n <= b。如果 a <b, 则 b <= n <= a。1 import random 2 print(random.uniform(1,10)) ...

从入门到精通Python学习第十七课:Python标准库【代码】【图】

简介 Python标准库使随着Python附带安装的,它包含很多有用的模块。所以对一个Python开发者来说,熟悉Python标准库是十分重要的。通过这些库中的模块,可以解决你的大部分问题。 sys模块 sys模块包含系统对应的功能。 import sysdef readfile(filename):'''Print a file to the standard output.'''f = open(filename)while True:line = f.readline()if len(line) == 0:breakprint(line)f.close()#Script starts from here if len(...

Python 标准库

https://docs.python.org/zh-cn/3/library/index.html

Python3 标准库:os【图】

1.重命名 import osos.rename('test.txt','x.txt') #重命名文件或目录import osos.renames('a/123.txt','a/b/h.txt') #递归重命名文件2.列出目录中的子目录和文件 import osfiles=os.listdir() for i in files:print(i) 输出结果:3.列出当前目录下的文件和文件夹以及文件夹下的内容 import osdef listDir(path):files=os.walk(path)for dirpath,dirnames,filenames in files:print('目录: %s'%dirpath)for filename in filename...