您的位置:首页 > 编程语言 > Python开发

Python学习——python的常用模块

2019-01-20 17:25 246 查看

最近开始整理python的资料,博主建立了一个qq群,希望给大家提供一个交流学习的平台78486745。

模块:用一堆代码实现了某个功能的代码集合,模块是不带 .py 扩展的另外一个 Python 文件的文件名。

一、time & datetime模块

import time
import datetime

print(time.asctime())      # 返回时间格式:Sun May  7 21:46:15 2017
print(time.time())         # 返回时间戳 ‘1494164954.6677325’
print(time.gmtime())       # 返回本地时间 的struct time对象格式,time.struct_time(tm_year=2017, tm_mon=5, tm_mday=7, tm_hour=22, tm_min=4, tm_sec=53, tm_wday=6, tm_yday=127, tm_isdst=0)
print(time.localtime())    # 返回本地时间 的struct time对象格式,time.struct_time(tm_year=2017, tm_mon=5, tm_mday=7, tm_hour=22, tm_min=4, tm_sec=53, tm_wday=6, tm_yday=127, tm_isdst=0)
print(time.gmtime(time.time()-800000))   # 返回utc时间的struc时间对象格式
print(time.asctime(time.localtime()))    # 返回时间格式Sun May  7 22:15:09 2017
print(time.ctime())                      # 返回时间格式Sun May  7 22:15:09 2017
print(time.strftime('%Y-%m-%d'))         #默认当前时间 2017-05-07
print(time.strftime('%Y-%m-%d',time.localtime())) #默认当前时间 2017-05-07

string_struct = time.strptime("2016/05/22","%Y/%m/%d") # 将日期字符串 转成 struct时间对象格式
print(string_struct)                     # 返回struct time对象格式 time.struct_time(tm_year=2016, tm_mon=5, tm_mday=22, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=143, tm_isdst=-1)

# 将日期字符串转成时间戳
struct_stamp = time.mktime(string_struct) # 将struct time时间对象转成时间戳
print(struct_stamp)                         # 返回时间戳 ‘1463846400.0’

# 将时间戳转为字符串格式
print(time.gmtime(time.time()-86640))         # 将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) # 将utc struct_time格式转成指定的字符串格式

# 时间加减
print(datetime.datetime.now())           # 返回当前时间 2017-05-07 22:36:45.179732
print(datetime.date.fromtimestamp(time.time()))  # 时间戳直接转换成日期格式 2017-05-07
print(datetime.datetime.now() + datetime.timedelta(3))    # 返回时间在当前日期上 +3 天
print(datetime.datetime.now() + datetime.timedelta(-3))    # 返回时间在当前日期上 -3 天
print(datetime.datetime.now() + datetime.timedelta(hours= 3)) # 返回时间在当前时间上 +3 小时
print(datetime.datetime.now() + datetime.timedelta(minutes= 30)) # 返回时间在当前时间上 +30 分钟

c_time  = datetime.datetime.now()
print(c_time)                          # 当前时间为 2017-05-07 22:52:44.016732
print(c_time.replace(minute=3,hour=2)) # 时间替换 替换时间为‘2017-05-07 02:03:18.181732’

print(datetime.timedelta)      # 表示时间间隔,即两个时间点之间的长度
print (datetime.datetime.now() - datetime.timedelta(days=5))  # 返回时间在当前时间上 -5 天

# python 日历模块
import calendar

print(calendar.calendar(theyear= 2017))     # 返回2017年整年日历
print(calendar.month(2017,5))               # 返回某年某月的日历,返回类型为字符串类型

calendar.setfirstweekday(calendar.WEDNESDAY) # 设置日历的第一天(第一天以星期三开始)
cal = calendar.month(2017, 4)
print (cal)

print(calendar.monthrange(2017,5))        # 返回某个月的第一天和这个月的所有天数
print(calendar.monthcalendar(2017,5))     # 返回某个月以每一周为元素的序列

cal = calendar.HTMLCalendar(calendar.MONDAY)
print(cal.formatmonth(2017, 5))           # 在html中打印某年某月的日历

print(calendar.isleap(2017))             # 判断是否为闰年
print(calendar.leapdays(2000,2017))       # 判断两个年份间闰年的个数

二、random模块

import random

# 随机数
print(random.random())              # 返回一个随机小数'0.4800545746046827'
print(random.randint(1,5))          # 返回(1-5)随机整型数据
print(random.randrange(1,10))       # 返回(1-10)随机数据

# 生成随机验证码
code = ''
for i in range(4):
current = random.randrange(0,4)
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
code += str(temp)

print(code)

三、OS模块

import os

print(os.getcwd())        # 获得当前工作目录
print(os.chdir("dirname")) # 改变当前脚本的工作路径,相当于shell下的cd
print(os.curdir)            # 返回当前目录‘.'
print(os.pardir)            # 获取当前目录的父目录字符串名‘..'
print(os.makedirs('dirname1/dirname2'))     # 可生成多层递归目录
print(os.removedirs('dirname1/dirname2'))      # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
print(os.mkdir('test4'))         # 生成单级目录;相当于shell中mkdir dirname
print(os.rmdir('test4'))        # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
print(os.listdir('/pythonStudy/s12/test'))   # 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
print(os.remove('log.log'))            # 删除一个指定的文件
print(os.rename("oldname","newname"))    # 重命名文件/目录)
print(os.stat('/pythonStudy/s12/test'))     # 获取文件/目录信息
print(os.pathsep)            # 输出用于分割文件路径的字符串';'
print(os.name)               # 输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
print(os.system(command='bash'))   # 运行shell命令,直接显示
print(os.environ)                  # 获得系统的环境变量
print(os.path.abspath('/pythonStudy/s12/test'))   # 返回path规范化的绝对路径
print(os.path.split('/pythonStudy/s12/test'))     # 将path分割成目录和文件名二元组返回
print(os.path.dirname('/pythonStudy/s12/test'))    # 返回path的目录。其实就是os.path.split(path)的第一个元素
print(os.path.basename('/pythonStudy/s12/test'))   # 返回path最后的文件名。如果path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
print(os.path.exists('test'))                 # 判断path是否存在
print(os.path.isabs('/pythonStudy/s12/test'))    # 如果path是绝对路径,返回True
print(os.path.isfile('test'))                   # 如果path是一个存在的文件,返回True。否则返回False
print(os.path.isdir('/pythonStudy/s12/test'))    # 如果path是一个存在的目录,则返回True。否则返回False
print(os.path.getatime('/pythonStudy/s12/test'))   # 返回path所指向的文件或者目录的最后存取时间
print(os.path.getmtime('/pythonStudy/s12/test'))   # 返回path所指向的文件或者目录的最后修改时间

四、sys模块

import sys

print(sys.argv)          # 命令行参数List,第一个元素是程序本身路径
print(sys.exit(n))     # 退出程序,正常退出时exit(0)
print(sys.version)       # 获取python的版本信息
print(sys.path)          # 返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
print(sys.platform)      # 返回操作平台的名称

五、shutil模块

import shutil
shutil.copyfileobj(fsrc, fdst, length=16*1024)      # 将文件内容拷贝到另一个文件中,可以是部分内容
shutil.copyfile(src, dst)                           # 拷贝文件
shutil.copymode(src, dst)                           # 仅拷贝权限。内容、组、用户均不变
shutil.copystat(src, dst)                           # 拷贝状态的信息,包括:mode bits, atime, mtime, flags
shutil.copy(src, dst)                               # 拷贝文件和权限
shutil.copy2(src, dst)                              # 拷贝文件和状态信息
shutil.move(src, dst)                               # 递归的去移动文件

# base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径
# format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
# root_dir: 要压缩的文件夹路径(默认当前目录)
# owner: 用户,默认当前用户
# group: 组,默认当前组
# logger: 用于记录日志,通常是logging.Logger对象
shutil.make_archive(base_name, format,root_dir,owner,group,logger)   # 创建压缩包并返回文件路径,例如:zip、tar

shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的:

# zipfile 压缩解压

import zipfile
# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()

# tarfile 压缩解压

import tarfile

# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
tar.close()

# 解压
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可设置解压地址
tar.close()

六、XML处理模块

# xml的格式如下,就是通过<>节点来区别数据结构的:
xmltest.xml

<?xml version="1.0"?>

<data>

<country name="Liechtenstein">

<rank updated="yes">2</rank>

<year>2008</year>

<gdppc>141100</gdppc>

<neighbor name="Austria" direction="E"/>

<neighbor name="Switzerland" direction="W"/>

</country>

<country name="Singapore">

<rank updated="yes">5</rank>

<year>2011</year>

<gdppc>59900</gdppc>

<neighbor name="Malaysia" direction="N"/>

</country>

<country name="Panama">

<rank updated="yes">69</rank>

<year>2011</year>

<gdppc>13600</gdppc>

<neighbor name="Costa Rica" direction="W"/>

<neighbor name="Colombia" direction="E"/>

</country>

</data>

#  xml协议在各个语言里的都 是支持的,在python中可以用以下模块操作xml  

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)

#遍历xml文档
for child in root:
print(child.tag, child.attrib)
for i in child:
print(i.tag,i.text)

#只遍历year 节点
for node in root.iter('year'):
print(node.tag,node.text)

# 修改和删除xml文档内容

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()

#修改

for node in root.iter('year'):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updated","yes")
tree.write("xmltest.xml")

#删除node

for country in root.findall('country'):
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write('output.xml')

# 自己创建xml文档
import xml.etree.ElementTree as ET

new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
age = ET.SubElement(name, "age", attrib={"checked": "no"})
age = ET.SubElement(name, "age")
age.text = '33'
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '19'
et = ET.ElementTree(new_xml)  # 生成文档对象
et.write("test.xml", encoding="utf-8", xml_declaration=True)
ET.dump(new_xml)  # 打印生成的格式

七、configparser模块

用于生成和修改常见配置文档

# 好多软件的常见文档格式如下
[DEFAULT]
compressionlevel = 9
serveraliveinterval = 45
compression = yes
forwardx11 = yes

[bitbucket.org]
user = hg

[topsecret.server.com]
host port = 50022
forwardx11 = no

# python 生成一个这样的文档

import configparser

config = configparser.ConfigParser()

config["DEFAULT"] = {'ServerAliveInterval': '45',
'Compression': 'yes',
'CompressionLevel': '9'}

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'
topsecret['ForwardX11'] = 'no'
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
20000

config.write(configfile)

# 写完了还可以再读出来

import configparser
config = configparser.ConfigParser()
config.sections()
file = config.read('example.ini')
print(file)      # ['example.ini']

title = config.sections()
print(title)     # ['bitbucket.org', 'topsecret.server.com']

print('bitbucket.org' in config)     # True
print('bytebong.com' in config)      # False
print(config['bitbucket.org']['User'])   # hg
print(config['DEFAULT']['Compression'])   # yes

topsecret = config['topsecret.server.com']
print(topsecret['ForwardX11'])           # no
print(topsecret['Host Port'])         # 50022

for key in config['topsecret.server.com']:
print(key)
'''
输出结果:
host port
forwardx11
compressionlevel
serveraliveinterval
compression
'''
print(config['topsecret.server.com']['Compression'])   # yes

# configparser增删改查语法
import configparser

config = configparser.ConfigParser()
config.read('i.cfg')

secs = config.sections()       # 返回配置文件中的主节点
print (secs)

options = config.options('bitbucket.org')
print(options)                 # 返回所有子节点信息

item_list = config.items('bitbucket.org')
print(item_list)              # 列出所有子节点详细信息

val = config.get('topsecret.server.com','host port')
print(val)                    # 返回单个子节点信息

val2 = config.getint('topsecret.server.com','host port')
print(val2)

# 删除'bitbucket.org'
sec = config.remove_section('bitbucket.org')
config.write(open('i.cfg','w'))

sec2 = config.add_section('huhuan2')      # 添加主节点
config.set('huhuan2','k','1111')          # 添加子节点
config.set('huhuan','kk','2222')
config.remove_option('huhuan','kk')       # 删除子节点
config.write(open('i.cfg','w'))

八、hashlib模块

用于加密相关的操作

import hashlib

# ****** md5 ******
m =hashlib.md5()
m.update(b'hello')
print(m.hexdigest())     # 16进制格式
print(m.digest())        # 2进制格式

# ****** shal ******
hash = hashlib.sha1()
hash.update(b'hello')
print(hash.hexdigest())

# ****** sha224 ******
hash = hashlib.sha224()
hash.update(b'hello')
print(hash.hexdigest())

# ****** sha256 ******
hash = hashlib.sha256()
hash.update(b'hello')
print(hash.hexdigest())

# ****** sha384 ******
hash = hashlib.sha384()
hash.update(b'hello')
print(hash.hexdigest())

# ****** sha512 ******
hash = hashlib.sha512()
hash.update(b'hello')
print(hash.hexdigest())

运行结果:
5d41402abc4b2a76b9719d911017c592
b']A@*\xbcK*v\xb9q\x9d\x91\x10\x17\xc5\x92'
aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
ea09ae9cc6768c50fcee903ed054556e5bfc8347907f12598aa24193
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
59e1748777448c69de6b800d7a33bbfb9ff1b463e44354c3553bcdb9c666fa90125a3c79f90397bdf5f6a13de828684f
9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043

九、logging 模块

python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug(), info(), warning(), error() and critical() 5个级别。

import logging

# %(message)s 日志信息
# %(levelno)s 日志级别
# datefmt  设置时间格式
# filename  设置日志保存的路径
# level=loggin.INFO意思是,把日志纪录级别设置为INFO,也就是说,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,
# 在这个例子, 第一条日志是不会被纪录的,如果希望纪录debug的日志,那把日志级别改成DEBUG就行了。
logging.basicConfig(format='%(asctime)s %(message)s %(levelno)s', datefmt='%m/%d/%Y %I:%M:%S %p',filename='example.log',level=logging.INFO)
logging.debug('This message should go to the log file')
logging.info('So should this')

日志格式

%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s 用户输出的消息
# 对于等级:

CRITICAL = 50

ERROR = 40

WARNING = 30

INFO = 20

DEBUG = 10

NOTSET = 0

Python 使用logging模块记录日志涉及四个主要类:

logger提供了应用程序可以直接使用的接口;

handler将(logger创建的)日志记录发送到合适的目的输出;

filter提供了细度设备来决定输出哪条日志记录;

formatter决定日志记录的最终输出格式。

logger :

每个程序在输出信息之前都要获得一个Logger。Logger通常对应了程序的模块名,比如聊天工具的图形界面模块可以这样获得它的Logger: LOG=logging.getLogger(”chat.gui”)

而核心模块可以这样: LOG=logging.getLogger(”chat.kernel”)

Logger.setLevel(lel):指定最低的日志级别,低于lel的级别将被忽略。debug是最低的内置级别,critical为最高

Logger.addFilter(filt)、Logger.removeFilter(filt):添加或删除指定的filter

Logger.addHandler(hdlr)、Logger.removeHandler(hdlr):增加或删除指定的handler

Logger.debug()、Logger.info()、Logger.warning()、Logger.error()、Logger.critical():可以设置的日志级别

handler

handler

对象负责发送相关的信息到指定目的地。Python的日志系统有多种Handler可以使用。有些Handler可以把信息输出到控制台,有些Logger可以把信息输出到文件,还有些 Handler可以把信息发送到网络上。如果觉得不够用,还可以编写自己的Handler。可以通过

addHandler()方法添加多个多handler

Handler.setLevel(lel):指定被处理的信息级别,低于lel级别的信息将被忽略

Handler.setFormatter():给这个handler选择一个格式

Handler.addFilter(filt)、Handler.removeFilter(filt):新增或删除一个filter对象

每个Logger可以附加多个Handler。接下来我们就来介绍一些常用的Handler:

  1. logging.StreamHandler
    使用这个Handler可以向类似与sys.stdout或者sys.stderr的任何文件对象(file object)输出信息。它的构造函数是:
    StreamHandler([strm])
    其中strm参数是一个文件对象。默认是sys.stderr

  2. logging.FileHandler
    和StreamHandler类似,用于向一个文件输出日志信息。不过FileHandler会帮你打开这个文件。它的构造函数是:
    FileHandler(filename[,mode])
    filename是文件名,必须指定一个文件名。
    mode是文件的打开方式。参见Python内置函数open()的用法。默认是’a’,即添加到文件末尾。

  3. logging.handlers.RotatingFileHandler
    这个Handler类似于上面的FileHandler,但是它可以管理文件大小。当文件达到一定大小之后,它会自动将当前日志文件改名,然后创建 一个新的同名日志文件继续输出。比如日志文件是chat.log。当chat.log达到指定的大小之后,RotatingFileHandler自动把 文件改名为chat.log.1。不过,如果chat.log.1已经存在,会先把chat.log.1重命名为chat.log.2。。。最后重新创建 chat.log,继续输出日志信息。它的构造函数是:
    RotatingFileHandler( filename[, mode[, maxBytes[, backupCount]]])
    其中filename和mode两个参数和FileHandler一样。
    maxBytes用于指定日志文件的最大文件大小。如果maxBytes为0,意味着日志文件可以无限大,这时上面描述的重命名过程就不会发生。
    backupCount用于指定保留的备份文件的个数。比如,如果指定为2,当上面描述的重命名过程发生时,原有的chat.log.2并不会被更名,而是被删除。

  4. logging.handlers.TimedRotatingFileHandler
    这个Handler和RotatingFileHandler类似,不过,它没有通过判断文件大小来决定何时重新创建日志文件,而是间隔一定时间就 自动创建新的日志文件。重命名的过程与RotatingFileHandler类似,不过新的文件不是附加数字,而是当前时间。它的构造函数是:
    TimedRotatingFileHandler( filename [,when [,interval [,backupCount]]])
    其中filename参数和backupCount参数和RotatingFileHandler具有相同的意义。
    interval是时间间隔。
    when参数是一个字符串。表示时间间隔的单位,不区分大小写。它有以下取值:
    S 秒
    M 分
    H 小时
    D 天
    W 每星期(interval==0时代表星期一)
    midnight 每天凌晨

import logging

logger = logging.getLogger('TEST_LOG')    # 获得一个Logger
logger.setLevel(logging.DEBUG)            # 设置日志级别

ch = logging.StreamHandler()            # logging.StreamHandler这个Handler可以向类似与sys.stdout或者sys.stderr的任何文件对象(file object)输出信息。
ch.setLevel(logging.DEBUG)

fh = logging.FileHandler("access.log")  # 用于向一个文件输出日志信息。不过FileHandler会帮你打开这个文件
fh.setLevel(logging.WARNING)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')  # 设置日志记录的最终输出格式

ch.setFormatter(formatter)
fh.setFormatter(formatter)

# 添加ch,fh到logger
logger.addHandler(ch)
logger.addHandler(fh)

logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

文件自动截断实例:

import logging
from logging import handlers

logger = logging.getLogger(__name__)
log_file = "timelog.log"
fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=3)
#fh = handlers.TimedRotatingFileHandler(filename=log_file,when="S",interval=5,backupCount=3)

formatter = logging.Formatter('%(asctime)s %(module)s:%(lineno)d %(message)s')
fh.setFormatter(formatter)

logger.addHandler(fh)

logger.warning("test1")
logger.warning("test12")
logger.warning("test13")
logger.warning("test14")

十、json & pickle 模块

用于序列化的两个模块

json,用于字符串 和 python数据类型间进行转换
pickle,用于python特有的类型 和 python的数据类型间进行转换
Json模块提供了四个功能:dumps、dump、loads、load

pickle模块提供了四个功能:dumps、dump、loads、load

import pickle

date = {'k1':'123','k2':'hello'}

str = pickle.dumps(date)    # pickle.dumps 将数据通过特殊的形式转换为只有python认识的字符串
print(str)

with open('result.pk','w') as fp:  # pickle.dump 将数据通过特殊的形式转换为只有python认识的字符串并写入文件
pickle.dump(date,fp)

import json

str1 = json.dumps(date)     # json.dumps 将数据通过特殊形式转换为所有程序语言都认识的字符串
print(str1)

with open('result1.json','w') as fp:  #json.dump 将数据通过特殊的形式转换为只有python认识的字符串并写入文件
json.dump(date,fp)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: