py_chunwei

导航

Python之旅Day6 模块应用

  time

  datetime

  random

  os

  sys

  shutil

  pickle

  json

  shelv

  xml

  configparser

  hashlib

  subprocess 

  logging

  re

  urllib 

  traceback

 

 

 

模块的介绍

  模块:简单粗暴的说就是用一砣代码实现了某个功能的代码集合。 

  类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。

如:os 是系统相关的模块;file是文件操作相关的模块

  模块分为三种:

  1)自定义模块

  2)内置标准模块(又称标准库或内置函数)

  3)开源模块

 

time & datetime模块 

  在Python中,通常有这几种方式来表示时间:

  1)时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。

  2)格式化的时间字符串(Format String)

  3)结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)

 

  time

##时间模块:
import time
#--------------------------我们先以当前时间为准,让大家快速认识三种形式的时间
print(time.time()) # 时间戳:1487130156.419527
print(time.strftime("%Y-%m-%d %X")) #格式化的时间字符串:'2017-02-15 11:40:53'
print(time.strftime("%Y{nian}-%m{yue}-%d{ri}").format(nian='',yue='',ri=''))  #自定义格式显示中文年月日
print(time.localtime()) #本地时区的struct_time
print(time.gmtime())    #UTC时区的struct_time

##运行结果:
C:\Python35\python.exe D:/Python代码目录/day5/time&datetime模块.py
1490859422.684743
2017-03-30 15:37:02
2017年-07月-28日
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=15, tm_min=37, tm_sec=2, tm_wday=3, tm_yday=89, tm_isdst=0)
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=7, tm_min=37, tm_sec=2, tm_wday=3, tm_yday=89, tm_isdst=0)

Process finished with exit code 0


###常见操作###
>>> import time

>>> print(time.clock())  #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来
1.3995892672030514e-06

>>> print(time.altzone)  #返回与utc时间的时间差,以秒计算
-32400

>>> print(time.asctime()) #返回时间格式“Thu Mar 30 15:47:30 2017”
Thu Mar 30 15:47:30 2017

>>> print(time.localtime())  #返回本地时间 的struct time对象格式
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=30, tm_hour=15, tm_min=51, tm_sec=18, tm_wday=3, tm_yday=89, tm_isdst=0)

>>> print(time.gmtime(time.time()-800000))  #返回utc时间的struc时间对象格式
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=21, tm_hour=1, tm_min=39, tm_sec=51, tm_wday=1, tm_yday=80, tm_isdst=0)

>>> print(time.asctime(time.localtime())) #返回时间格式“Thu Mar 30 15:54:42 2017”
Thu Mar 30 15:54:42 2017

>>> print(time.ctime()) #返回个事同上
Thu Mar 30 15:55:55 2017



###日期字符串转成时间戳:
string_2_struct = time.strptime("2016/05/22","%Y/%m/%d") #将 日期字符串 转成 struct时间对象格式
print(string_2_struct)
struct_2_stamp = time.mktime(string_2_struct)  #将struct时间对象转成时间戳
print(struct_2_stamp)

##结果:
C:\Python35\python.exe D:/Python代码目录/day5/time&datetime模块.py
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)
1463846400.0

Process finished with exit code 0


###将时间戳转为字符串格式
>>> import time
>>> print(time.gmtime(time.time()-86640))  #将utc时间戳转换成struct_time格式
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=29, tm_hour=8, tm_min=49, tm_sec=51, tm_wday=2, tm_yday=88, tm_isdst=0)

>>> print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将utc struct_time格式转成指定的字符串格式
2017-03-30 08:54:06

  datetime

###基本操作###
import datetime
i = datetime.datetime.now()
print ("当前的日期和时间是: %s" % i) #当前的日期和时间是: 2017-07-28 18:03:58.293743
print ("ISO格式的日期和时间是: %s" % i.isoformat() ) #ISO格式的日期和时间是: 2017-07-28T18:03:58.293743
print ("当前的年份是: %s" %i.year)    #当前的年份是: 2017
print ("当前的月份是: %s" %i.month)   #当前的月份是: 7
print ("当前的日期是:  %s" %i.day)    #当前的日期是:  28
print ("dd/mm/yyyy 格式是:  %s/%s/%s" % (i.day, i.month, i.year) ) #dd/mm/yyyy 格式是:  28/7/2017
print ("当前小时是: %s" %i.hour) #当前小时是: 18
print ("当前分钟是: %s" %i.minute)   #当前分钟是: 3
print ("当前秒是:  %s" %i.second)   #当前秒是:  58
print("%s年%s月%s日%s时%s分%s秒"%(i.year,i.month,i.day,i.hour,i.minute,i.second))   #2017年7月28日18时10分47秒



>>> import datetim

>>> print(datetime.datetime.now())
2017-03-30 16:58:44.974137
>>> 
>>> print(datetime.date.fromtimestamp(time.time()) )
2017-03-30
>>> 
>>> print(datetime.datetime.now() )
2017-03-30 16:59:11.777671
>>> 
>>> print(datetime.datetime.now() + datetime.timedelta(3))  #当前时间+3天
2017-04-02 16:59:25.067431
>>> 
>>> print(datetime.datetime.now() + datetime.timedelta(-3)) 当前时间-3天
2017-03-27 16:59:37.486141
>>> 
>>> print(datetime.datetime.now() + datetime.timedelta(hours=3))  #当前时间+3小时
2017-03-30 19:59:51.665952
>>> 
>>> print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
2017-03-30 17:30:03.384622
>>> 

>>> c_time  = datetime.datetime.now()  #时间替换
>>> print(c_time.replace(minute=3,hour=2))
2017-03-30 02:03:28.892945

 

random模块

  random主要用来生成随机数

print (random.random()) #随机生成一个float数
print (random.randint(1,5)) #[1,5]在包含1-5之间随机生成一个int数
print (random.randrange(1,10))  #[1,10)在1-10但不包含10之间随机生成一个int数
print(random.randrange(1,20,2)) #[1,20)随机生成一个大于等于1小于20的int数,步长2,永远不会出现2N(偶数)
print(random.sample(range(100),5))  #在0-99之间随机生成5个数
print(random.sample([1,'23',[4,5]],2))  #1,'23',[4,5]随机2个数组合生成一个新列表
print(random.choice([1,'23',[4,5]]))    #随机打印1,'23',[4,5]三个数中的一个
print(random.uniform(1,5))  #随机生成一个大于1小于5的float数



item=[1,3,5,7,9]
random.shuffle(item)    #按随机顺序打印原数据
print(item)
###随机生成字符串###

#方法一
import random
checkcode = ''
for i in range(6):  #此处可以定义随机数的长短位数
    current = random.randrange(0,4)
    if current != i:
        temp = chr(random.randint(65,90))    #65-90是ascii对应的a-Z
    else:
        temp = random.randint(0,9)
    checkcode += str(temp)
print (checkcode)



#方法二:精简版
source = string.digits + string.ascii_lowercase
print("".join(random.sample(source,6)))
随机生成自定义位数的验证码

 

os模块

  os模块是与操作系统交互的一个接口

import  os,sys
print(globals())
'''
{'__doc__': None, '__spec__': None, 'sys': <module 'sys' (built-in)>, 'os': <module 'os' from 'C:\\Python34\\lib\\os.py'>, '__name__': '__main__', '__loader__': <_frozen_importlib.SourceFileLoader object at 0x0000000001D9AC50>, '__builtins__': <module 'builtins' (built-in)>, '__cached__': None, '__package__': None, '__file__': 'E:/Python16/Python16_code/day05/os模块.py'}
'''

file_path = os.path.abspath(__file__)


print(os.getcwd())
'''
E:\Python16\Python16_code\day05
'''

print(os.path.split(file_path)) ''' ('E:\\Python16\\Python16_code\\day05', 'os模块.py') '''
print(os.path.dirname(file_path) ) ''' E:\Python16\Python16_code\day05 '''
print(os.path.dirname(os.path.dirname(file_path))) ''' E:\Python16\Python16_code '''
print(os.path.basename(os.path.dirname(file_path))) ''' day05 '''
print(os.path.join("c:\\","programs","python27")) ''' c:\programs\python27 ''' join()函数 ''' 语法:'sep'.join(seq) 参数说明 sep:分隔符。可以为空 seq:要连接的元素序列、字符串、元组、字典 ''' ###对序列进行操作(以 '.'为分隔符) seq = ['hello','good','boy','doiido'] print('.'.join(seq)) hello.good.boy.doiido ###对元组进行操作(以 ':'为分隔符) seq = ('hello','good','boy','doiido') print(':'.join(seq)) hello:good:boy:doiido ###对字典进行操作 seq = {'hello':1,'good':2,'boy':3,'doiido':4} print(':'.join(seq)) doiido:boy:hello:good

os模块的常见操作:

os.getcwd() #获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")  #改变当前脚本工作目录;相当于shell下cd
os.popen('dir').read() #获取命令dir调用的结果
os.curdir  #返回当前目录: ('.')
os.pardir  #获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')    #可生成多层递归目录
os.makedirs('dirname1/dirname2',exist_ok=True) #存在也不会报错
os.removedirs('dirname1')    #若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.mkdir('dirname')    #生成单级目录;相当于shell中mkdir dirname
os.rmdir('dirname')   # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.listdir('dirname')    #列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove()  #删除一个文件
os.rename("oldname","newname")  #重命名文件/目录
os.stat('path/filename')  #获取文件/目录信息
os.stat('file').st_size #获取file文件大小
os.sep    #输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep    #输出当前平台使用的行终止符,win下为"\t\n",Linux下为"\n"
os.pathsep    #输出用于分割文件路径的字符串 win下为;,Linux下为:
os.name    #输出字符串指示当前使用平台。win->'nt'; Linux->'posix'
os.system("bash command")  #运行shell命令,直接显示
os.environ  #获取系统环境变量
os.path.abspath(path)  #返回path规范化的绝对路径
os.path.split(path)  #将path分割成目录和文件名二元组返回
os.path.dirname(path)  #返回path的目录。其实就是os.path.split(path)的第一个元素
os.path.basename(path)  #返回path最后的文件名。如何path以/或\结尾,那么就会返回空值。即os.path.split(path)的第二个元素
os.path.exists(path)  #如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path)  #如果path是绝对路径,返回True
os.path.isfile(path)  #如果path是一个存在的文件,返回True。否则返回False
os.path.isdir(path)   #如果path是一个存在的目录,则返回True。否则返回False
os.path.join(path1[, path2[, ...]])  #将多个路径组合后返回,第一个绝对路径之前的参数将被忽略
os.path.getatime(path)  #返回path所指向的文件或者目录的最后存取时间
os.path.getmtime(path)  #返回path所指向的文件或者目录的最后修改时间
os.path.normpath(path)  #规范化路径,转换path的大小写和斜杠

 

sys模块

sys.argv            #命令行参数List,第一个元素是程序本身路径
sys.exit(n)         #退出程序,正常退出时exit(0)
sys.version         #获取Python解释程序的版本信息
sys.maxint          #最大的Int值
sys.path            #返回模块的搜索路径,初始化时使用PYTHONPATH环境变量的值
sys.platform        #返回操作系统平台名称
sys.stdout.write('please:')
val = sys.stdin.readline()[:-1]



>>> import sys
>>> sys.argv
['D:\\Program Files (x86)\\JetBrains\\PyCharm 5.0.5\\helpers\\pydev\\pydevconsole.py', '58642', '58643']
>>> 
>>> sys.version 
'3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]'
>>> 
>>> sys.path 
['D:\\Program Files (x86)\\JetBrains\\PyCharm 5.0.5\\helpers\\pydev', 'D:\\Program Files (x86)\\JetBrains\\PyCharm 5.0.5\\helpers\\pydev', 'C:\\Python35\\python35.zip', 'C:\\Python35\\DLLs', 'C:\\Python35\\lib', 'C:\\Python35', 'C:\\Python35\\lib\\site-packages', 'D:\\Python代码目录']
>>> 
import sys,time

for i in range(50):
    sys.stdout.write('%s\r' %('#'*i))
    sys.stdout.flush()
    time.sleep(0.1)

"""
注意:在pycharm中执行无效,请到命令行中以脚本的方式执行
"""
模拟进度条

 

shutil模块

  高级的文件、文件夹、压缩包处理模块

shutil.copyfileobj(fsrc, fdst[, length])
将文件内容拷贝到另一个文件中

import shutil

shutil.copyfileobj(open('shutil','r'), open('shutil_new', 'w')) #将shutil的内容拷贝至新文件shutil_new中

shutil.copyfile(src, dst)
拷贝文件

import shutil

shutil.copyfile('f1.log', 'f2.log')  ##目标文件无需存在,如果存在则内容会被覆盖

shutil.copymode(src, dst)
仅拷贝权限。内容、组、用户均不变

shutil.copymode('f1.log', 'f2.log') #目标文件必须存在

shutil.copystat(src, dst)
仅拷贝状态的信息,包括:mode bits, atime, mtime, flags

shutil.copystat('f1.log', 'f2.log') #目标文件必须存在

shutil.copy(src, dst)
拷贝文件和权限

import shutil 
shutil.copy('f1.log', 'f2.log')

shutil.copy2(src, dst)
拷贝文件和状态信息

import shutil 
shutil.copy2('f1.log', 'f2.log')

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
递归的去拷贝文件夹

import shutil
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目标目录不能存在
#注意对folder2目录父级目录要有可写权限,ignore的意思是排除 

import shutil

shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
#通常的拷贝都把软连接拷贝成硬链接,即对待软连接来说,创建新的文件

shutil.rmtree(path[, ignore_errors[, onerror]])
递归的去删除文件

import shutil
shutil.rmtree('folder1')

shutil.move(src, dst)
递归的去移动文件,它类似mv命令,其实就是重命名

import shutil
shutil.move('folder1', 'folder3')

shutil.make_archive(base_name, format,...)

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

#将 /data 下的文件打包放置当前程序目录
import shutil
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
  
  
#将 /data下的文件打包放置 /tmp/目录
import shutil
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')

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(path='.')
z.close()



###tarfile压缩解压缩###
import tarfile

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

# 解压
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可设置解压地址 
tar.extract('1.log') #将只解压压缩包里的1.log这一个文件(path="..." 用以指定解压路径)
tar.close()

 

json和pickle模块 

  文件存储只能接受字符串和bytes类型(不能接受写入内存数据 类型)

序列化:内存数据转化为字符串类型 (str)
反序列化:字符串数据转化为内存数据类型 (eval)

普通方式存储文件
account={
    "id":66586,
    "edu":18000,
    "yue":16888,
    "gqsj":"2018-01-20",
    "pwd":"hello666"

}

f=open("file.txt","w")
#f.write(account) #会报错 [文件存储只能接受字符串和bytes类型(不能接受写入内存数据 类型)]
f.write(str(account))
f.close()

##顺利运行,并在当前目录下生成内容为"{'gqsj': '2018-01-20', 'yue': 16888, 'pwd': 'hello666', 'id': 66586, 'edu': 18000}"的file.ttxt文件

    将存储的文件读取出来

###会报错的
f=open("file.txt","r")

account = f.read()

print(account)#成功输出;{'gqsj': '2018-01-20', 'yue': 16888, 'pwd': 'hello666', 'id': 66586, 'edu': 18000}
print(account['id'])#报错;TypeError: string indices must be integers


###成功执行的
f=open("file.txt","r")

account=eval(f.read())#将对象转为内存数据类型

print(account)#成功输出;{'gqsj': '2018-01-20', 'yue': 16888, 'pwd': 'hello666', 'id': 66586, 'edu': 18000}
print(account['id']) #成功输出ID;66586

  这种通过人为数据类型转换方式确实可以成功的存储或读取文件,但是有很多弊端;为此,做类似操作时,我们可以用pickle(Python特有)JSON(通用)这两个模块

 

  json和pickle用来做序列化的两个常用模块【我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化

  1)json:用于字符串 和 python数据类型间进行转换

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

  2)pickle:用于python特有的类型 和 python的数据类型间进行转换

      pickle模块提供了四个功能:dumps、dump、loads、load  【序列化出来的文件人类不可读,但不影响反序列化】

 

pickle实例:

   pickle序列化与反序列化

###序列化
import pickle

account={
    "id":66586,
    "edu":18000,
    "yue":16888,
    "gqsj":"2018-01-20",
    "pwd":"hello666"

}

f = open("file.db","wb")

f.write(pickle.dumps(account))    #等价于pickle.dump(account,f)

f.close()



###反序列化
import pickle

f = open("file.db","rb")

account = pickle.loads(f.read())    #等价于account = pickle.load(f)

print(account)
print(account['id'])
"""运行结果:
C:\Python35\python.exe D:/Python代码目录/Python_codeing/Python16_code/day05/pickle加载.py
{'pwd': 'hello666', 'gqsj': '2018-01-20', 'edu': 18000, 'id': 66586, 'yue': 16888}
66586

Process finished with exit code 0
"""

  pickle其他解释说明

import pickle
 
dic={'name':'alvin','age':23,'sex':'male'}
 
print(type(dic))#<class 'dict'>
 
j=pickle.dumps(dic)
print(type(j))#<class 'bytes'>
 
 
f=open('序列化对象_pickle','wb')#注意是w是写入str,wb是写入bytes,j是'bytes'
f.write(j)  #-------------------等价于pickle.dump(dic,f)
 
f.close()
#-------------------------反序列化
import pickle
f=open('序列化对象_pickle','rb')
 
data=pickle.loads(f.read())#  等价于data=pickle.load(f)
 
 
print(data['age'])


#Pickle的问题和所有其他编程语言特有的序列化问题一样...
#...就是它只能用于Python...
#...并且可能不同版本的Python彼此都不兼容...
#...因此,只能用Pickle保存那些不重要的数据...
#...不能成功地反序列化也没关系。

 

json实例:

 

  json的序列化与反序列化

###序列化

import json

account={
    "id":66586,
    "edu":18000,
    "yue":16888,
    "gqsj":"2018-01-20",
    "pwd":"hello666"

}

f = open("file.log","w")    #json方式序列化(存储类型人类可读,无"乱码")

f.write(json.dumps(account))

f.close()



###反序列化

import json
f = open("file.log","r")

account = json.loads(f.read()) #等价于account = json.load(f)

print(account)
print(account['id'])
'''运行结果:
C:\Python35\python.exe D:/Python代码目录/Python_codeing/Python16_code/day05/pickle&json加载.py
{'yue': 16888, 'gqsj': '2018-01-20', 'id': 66586, 'edu': 18000, 'pwd': 'hello666'}
66586

Process finished with exit code 0
'''

  json的其他解释说明

import json
 
dic={'name':'alvin','age':23,'sex':'male'}
print(type(dic))#<class 'dict'>
 
j=json.dumps(dic)
print(type(j))#<class 'str'>
 
 
f=open('序列化对象','w')
f.write(j)  #-------------------等价于json.dump(dic,f)
f.close()
#-----------------------------反序列化<br>
import json
f=open('序列化对象')
data=json.loads(f.read())#  等价于data=json.load(f)

 

shelv模块

  shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写;key必须为字符串,而值可以是python所支持的数据类型

import shelve

f=shelve.open(r'sheve.txt')
f['stu1_info']={'name':'egon','age':18,'hobby':['sing','smoking','drinking']}
f['stu2_info']={'name':'gangdan','age':53}
f['school_info']={'website':'http://www.pypy.org','city':'beijing'}

print(f['stu1_info']['hobby'])
print(f['stu2_info']['age'])
print(f['school_info']['city'])
f.close()

##结果生成shlve.txt.bak shlve.txt.dat shlve.txt.dir三个文件,内容如下:
'stu1_info', (0, 99)
'school_info', (1024, 73)
'stu2_info', (512, 45)

 

xml模块

  xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的接口还主要是xml。

  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格式数据

  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,child.attrib['name'])
    for i in child:
        print(i.tag,i.attrib,i.text)
 
#只遍历year 节点
for node in root.iter('year'):
    print(node.tag,node.text)
#---------------------------------------

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')
    node.set('version','1.0')
tree.write('test.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"})
sex = ET.SubElement(name,"sex")
sex.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) #打印生成的格式

###运行结果###:
C:\Python35\python.exe D:/Python代码目录/day5/xml模块_创建xml文件.py
<namelist><name enrolled="yes"><age checked="no" /><sex>33</sex></name><name enrolled="no"><age>19</age></name></namelist>

Process finished with exit code 0


###生成样式###:
<?xml version='1.0' encoding='utf-8'?>
<namelist><name enrolled="yes"><age checked="no" /><sex>33</sex></name><name enrolled="no"><age>19</age></name></namelist>

 

configparser模块

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

  好多软件的常见文档格式如下(常见的如php.ini就是如此):

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
 
[bitbucket.org]
User = hg
 
[topsecret.server.com]
Port = 50022
ForwardX11 = no

  在Python中用configparser生成一个配置文件

###生成代码###:
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'     # mutates the parser
topsecret['ForwardX11'] = 'no'       # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
   config.write(configfile)

###运行结果###:
C:\Python35\python.exe D:/Python代码目录/day5/8.configparser模块/8.configparser模块.py

Process finished with exit code 0

###生成样式###:
[DEFAULT]
compressionlevel = 9
compression = yes
serveraliveinterval = 45
forwardx11 = yes

[bitbucket.org]
user = hg

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

   configparser获取操作

# 获取所有节点
import configparser
config=configparser.ConfigParser()
config.read('example.ini',encoding='utf-8')
res=config.sections()
print(res)

C:\Python35\python.exe D:/Python代码目录/day5/8.configparser模块/查询操作.py
['bitbucket.org', 'topsecret.server.com']

Process finished with exit code 0


#获取指定节点下所有的键值对
import configparser
config=configparser.ConfigParser()
config.read('example.ini',encoding='utf-8')
res=config.items('bitbucket.org')
print(res)

C:\Python35\python.exe D:/Python代码目录/day5/8.configparser模块/查询操作.py
[('compressionlevel', '9'), ('compression', 'yes'), ('serveraliveinterval', '45'), ('forwardx11', 'yes'), ('user', 'hg')]

Process finished with exit code 0


#获取指定节点下所有的建(包含DEFAULT以及bitbucket.org这俩标题下所有的键)
import configparser
config=configparser.ConfigParser()
config.read('example.ini',encoding='utf-8')
res=config.options('bitbucket.org')
print(res)

C:\Python35\python.exe D:/Python代码目录/day5/8.configparser模块/查询操作.py
['user', 'compressionlevel', 'compression', 'serveraliveinterval', 'forwardx11']

Process finished with exit code 0

>>> import configparser >>> config = configparser.ConfigParser() >>> config.sections() [] >>> config.read('example.ini') ['example.ini'] >>> config.sections() ['bitbucket.org', 'topsecret.server.com'] >>> 'bitbucket.org' in config True >>> 'bytebong.com' in config False >>> config['bitbucket.org']['User'] 'hg' >>> config['DEFAULT']['Compression'] 'yes' >>> topsecret = config['topsecret.server.com'] >>> topsecret['ForwardX11'] 'no' >>> topsecret['Port'] '50022' >>> for key in config['bitbucket.org']: print(key) ... user compressionlevel serveraliveinterval compression forwardx11 >>> config['bitbucket.org']['ForwardX11'] 'yes'
###检查|删除|添加节点###
import configparser
config=configparser.ConfigParser()
config.read('test.ini',encoding='utf-8')

#检查
has_sec=config.has_section('bitbucket.org')
print(has_sec) #打印True

#添加节点
config.add_section('egon') #已经存在则报错
config['egon']['username']='gangdan'
config['egon']['age']='18'
config.write(open('test.ini','w'))

#删除节点
config.remove_section('egon')
config.write(open('test.ini','w'))
###检查|删除|设置指定组内的键值对###

import configparser
config=configparser.ConfigParser()
config.read('test.ini',encoding='utf-8')

#检查
has_sec=config.has_option('bitbucket.org','user') #bitbucket.org下有一个键user
print(has_sec) #打印True

#删除
config.remove_option('DEFAULT','forwardx11')
config.write(open('test.ini','w'))

#设置
config.set('bitbucket.org','user','gangdang')
config.write(open('test.ini','w'))

 

hashlib模块

  用于加密相关的操作,3.x里代替了md5模块和sha模块,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 算法。

  具有如下3个特点:

    1)内容相同则hash运算结果相同,内容稍微改变则hash值则变
    2)不可逆推
    3)相同算法:无论校验多长的数据,得到的哈希值长度固定

  常见的算法:

import hashlib

######## md5 ########
hash = hashlib.md5()
hash.update('admin'.encode('utf-8'))
print("md5: ",hash.hexdigest())

######## sha1 ########
hash = hashlib.sha1()
hash.update('admin'.encode('utf-8'))
print("sha1: ",hash.hexdigest())  

######## sha256 ########
hash = hashlib.sha256()
hash.update('admin'.encode('utf-8'))
print("sha256:",hash.hexdigest())   #

######## sha384 ########
hash = hashlib.sha384()
hash.update('admin'.encode('utf-8'))
print("sha384: ",hash.hexdigest())

######## sha512 ########
hash = hashlib.sha512()
hash.update('admin'.encode('utf-8'))
print("sha512: ",hash.hexdigest())


###运行结果###:
C:\Python35\python.exe D:/Python代码目录/day5/9.hashlib模块.py
md5:  21232f297a57a5a743894a0e4a801fc3
sha1:  d033e22ae348aeb5660fc2140aec35850c4da997
sha256: 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
sha384:  9ca694a90285c034432c9550421b7b9dbd5c0f4b6673f05f6dbce58052ba20e4248041956ee8c9a2ec9f10290cdc0782
sha512:  c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec

Process finished with exit code 0


'''
注意:把一段很长的数据update多次,与一次update这段长数据,得到的结果一样
但是update多次为校验大文件提供了可能。
 '''

import hashlib 
m=hashlib.md5() 

m.update('hello'.encode('utf-8')) 
print(m.hexdigest()) #5d41402abc4b2a76b9719d911017c592  

m.update('alvin'.encode('utf8')) 
print(m.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af  

m2=hashlib.md5() m2.update('helloalvin'.encode('utf8')) 
print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af 

  python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密,散列消息鉴别码,简称HMAC,是一种基于消息鉴别码MAC(Message Authentication Code)的鉴别机制;使用HMAC时,消息通讯的双方,通过验证消息中加入的鉴别密钥K来鉴别消息的真伪。

import hmac
h = hmac.new('alvin'.encode('utf8'))
h.update('hello'.encode('utf8'))
print ("hmac: ",h.hexdigest()) 


#hmac: 320df9832eab4c038b6c1d7ed73a5940

 

subprocess模块

    Python引入subprocess模块来管理子进程,以取代一些旧模块的方法:如 os.system、os.spawn*、os.popen*、popen2.*、commands.*不但可以调用外部的命令作为子进程,而且可以连接到子进程的input/output/error管道,获取相关的返回信息;subprocess包主要功能是执行外部的命令和程序。
    
    语法:subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, shell=False, timeout=None, check=False)

 

常见subprocess方法事例

#执行命令,返回命令执行状态 , 0 or 非0
>>> retcode = subprocess.call(["ls", "-l"])

#执行命令,如果命令结果为0,就正常返回,否则抛异常
>>> subprocess.check_call(["ls", "-l"])
0

#接收字符串格式命令,返回元组形式,第1个元素是执行状态,第2个是命令结果
>>> subprocess.getstatusoutput('ls /bin/ls')
(0, '/bin/ls')

#接收字符串格式命令,并返回结果
>>> subprocess.getoutput('ls /bin/ls')
'/bin/ls'

#执行命令,并返回结果,注意是返回结果,不是打印,下例结果返回给res
>>> res=subprocess.check_output(['ls','-l'])
>>> res
b'total 0\ndrwxr-xr-x 12 alex staff 408 Nov 2 11:05 OldBoyCRM\n'

  上述方法,都是底层封装的subprocess.Popen

###其他方法:
poll()
Check if child process has terminated. Returns returncode

wait()
Wait for child process to terminate. Returns returncode attribute.


terminate() 杀掉所启动进程
communicate() 等待任务结束

stdin 标准输入

stdout 标准输出

stderr 标准错误

pid
The process ID of the child process.



###其他事例:
>>> p = subprocess.Popen("df -h|grep disk",stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
>>> p.stdout.read()
b'/dev/disk1 465Gi 64Gi 400Gi 14% 16901472 104938142 14% /\n'

>>> subprocess.run(["ls", "-l"])  # doesn't capture output
CompletedProcess(args=['ls', '-l'], returncode=0)

>>> subprocess.run("exit 1", shell=True, check=True)
Traceback (most recent call last):
  ...
subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1

>>> subprocess.run(["ls", "-l", "/dev/null"], stdout=subprocess.PIPE)
CompletedProcess(args=['ls', '-l', '/dev/null'], returncode=0,
stdout=b'crw-rw-rw- 1 root root 1, 3 Jan 23 16:23 /dev/null\n')



###调用subprocess.run(...)是推荐的常用方法,在大多数情况下能满足需求,但如果你可能需要进行一些复杂的与系统的交互的话,你还可以用subprocess.Popen(),语法如下:
>>> a = subprocess.Popen("df -h|grep tmpfs",stdin=subprocess.PIPE,stdout=subprocess.PIPE,shell=True)
>>> print(a.stdout.read())
b'tmpfs           3.8G     0  3.8G   0% /dev/shm\n'
>>>

p = subprocess.Popen("find / -size +1000000 -exec ls -shl {} \;",shell=True,stdout=subprocess.PIPE)
print(p.stdout.read())

常见参数

    args:#shell命令,可以是字符串或者序列类型(如:list,元组)
    bufsize:#指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
    stdin, stdout, stderr:#分别表示程序的标准输入、输出、错误句柄
    preexec_fn:#只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
    close_sfs:#在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
    所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
    shell:#同上
    cwd:#用于设置子进程的当前目录
    env:#用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
    universal_newlines:#不同系统的换行符不同,True -> 同意使用 \n
    startupinfo与createionflags只在windows下有效
    将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等


终端输入的命令分为两种:

输入即可得到输出,如:ifconfig
输入进行某环境,依赖再输入,如:python
import subprocess

    obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    obj.stdin.write('print 1 \n ')
    obj.stdin.write('print 2 \n ')
    obj.stdin.write('print 3 \n ')
    obj.stdin.write('print 4 \n ')

    out_error_list = obj.communicate(timeout=10)
    print out_error_list
subprocess交互命令事例
    import subprocess

    def mypass():
        mypass = '123' #or get the password from anywhere
        return mypass

    echo = subprocess.Popen(['echo',mypass()],
                            stdout=subprocess.PIPE,
                            )

    sudo = subprocess.Popen(['sudo','-S','iptables','-L'],
                            stdin=echo.stdout,
                            stdout=subprocess.PIPE,
                            )

    end_of_pipe = sudo.stdout

    print ("Password ok \n Iptables Chains %s" % end_of_pipe.read())
subprocess实现sudo 自动输入密码

 

logging模块

  程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误、警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,logging的日志可以分为 debug()info()warning()error() and critical() 5个级别。

  日志级别解析:

  基本使用事例:

###输出到当前屏幕###
>>> import logging

>>> logging.warning("user [wu] attempted wrong password more then 3 times")
WARNING:root:user [wu] attempted wrong password more then 3 times #输出的结果
>>> 
>>> logging.critical("server is down") CRITICAL:root:server is down #输出的结果 
>>>

###输出到文件###
##:代码
import logging

logging.basicConfig(filename='example.log',level=logging.INFO)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this,too')
##运行:
C:\Python35\python.exe D:/Python代码目录/day5/11.logging模块/11.logging模块.py

Process finished with exit code 0
##日志内容:
INFO:root:So should this
WARNING:root:And this,too

'''
这句中的level=loggin.INFO意思是,把日志纪录级别设置为INFO,也就是说,只有比日志是INFO或比INFO级别更高的日志才会被纪录到文件里,在这个例子, 第一条日志是不会被纪录的,如果希望纪录debug的日志,那把日志级别改成DEBUG就行了
#即将logging.basicConfig(filename='example.log',level=logging.INFO)改为logging.basicConfig(filename='example.log',level=logging.INFO)即可
'''

###加上记录日志的时间###
##代码:
import logging

logging.basicConfig(filename='example.log',
            level=logging.INFO,
            format='%(asctime)s %(message)s',
            datefmt='%m/%d/%Y %I:%M:%S %p'
          ) logging.debug('add time at after,This message should go to the log file') logging.info('add time at after,So should this') logging.warning('add time at after,And this,too') ##运行: C:\Python35\python.exe D:/Python代码目录/day5/11.logging模块/11.logging模块.py Process finished with exit code 0 ##日志内容: 04/12/2017 11:21:09 AM add time at after,So should this 04/12/2017 11:21:09 AM add time at after,And this,too """logging模块生成后的内容记录到文件中默认都是追加的形式 """

  日志格式:

  logging模块日志记录的分类:

    1)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():可以设置的日志级别
logger

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

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 每天凌晨
handler

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

    4)formatter:决定日志记录的最终输出格式

  

  logging模块记录日志到文件并在屏幕输出:

###代码:
import logging
 
#create logger
logger = logging.getLogger('TEST-LOG')
logger.setLevel(logging.DEBUG)
 
 
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
 
# create file handler and set level to warning
fh = logging.FileHandler("access.log")
fh.setLevel(logging.WARNING)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
 
# add formatter to ch and fh
ch.setFormatter(formatter)
fh.setFormatter(formatter)
 
# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)
 
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warn('warn message')
logger.error('error message')
logger.critical('critical message')

###运行:
C:\Python35\python.exe D:/Python代码目录/day5/11.logging模块/logging2.py
2017-04-12 11:38:56,101 - TEST-LOG - DEBUG - debug message
2017-04-12 11:38:56,101 - TEST-LOG - INFO - info message
2017-04-12 11:38:56,102 - TEST-LOG - WARNING - warn message
2017-04-12 11:38:56,102 - TEST-LOG - ERROR - error message
2017-04-12 11:38:56,102 - TEST-LOG - CRITICAL - critical message

Process finished with exit code 0

##记录文件:
2017-04-12 11:38:56,102 - TEST-LOG - WARNING - warn message
2017-04-12 11:38:56,102 - TEST-LOG - ERROR - error message
2017-04-12 11:38:56,102 - TEST-LOG - 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)     #按自定义时间切割:when="S",以秒为单位切割;interval=5,每个5秒切割一次;backupCount=3,保留最新的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")
日志按需切割
import logging

'''
日志模式:
logging.debug('debug')
logging.info('info')
logging.warning('warning')
logging.error('error')
logging.critical('critical')
logging.log(10, 'log')

日志级别:
    CRITICAL = 50
    FATAL = CRITICAL
    ERROR = 40
    WARNING = 30
    WARN = WARNING
    INFO = 20
    DEBUG = 10
    NOTSET = 0
'''

"""
logging.log(10, 'log12345') 等价于logging.debug('log12345')
"""

###logging.basicConfig只能写入到一个文件,多次申明(定义多次)或调用(写多个函数去调用)无效###
logging.basicConfig(filename='log1.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=logging.INFO#推荐写法      #level=10
                    )

logging.basicConfig(filename='log2.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=logging.INFO#推荐写法      #level=10
                    )

#只会生成一个文件log1.log(通过函数的方法也只会产生一个文件,会写两次)
logging.log(logging.ERROR,'sttyle is error:hjfgasdfgshdgf')#推荐写法     # logging.error('sttyle is error:hjfgasdfgshdgf')
单文件写入——logging.basicConfig
import logging


#logging.FileHandler多文件写入日志#
#定义操作文件的对象
file_1 = logging.FileHandler('fh1.log', 'a', encoding='utf-8')
fmt = logging.Formatter(fmt="%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s")#自定义format格式
file_1.setFormatter(fmt)

file_2 = logging.FileHandler('fh2.log', 'a', encoding='utf-8')
fmt = logging.Formatter()#format默认格式
file_2.setFormatter(fmt)

#创建日志对象
logger1 = logging.Logger('s1', level=logging.ERROR)
#日志对象和文件多项创建关系
logger1.addHandler(file_1)
logger1.addHandler(file_2)


# 写日志(会生成fh1.log和fh2.log两个文件)
logger1.log(logging.CRITICAL,'most imort for level critical')   # logger1.critical('most imort for level critical')
多文件写入——logging.FileHandler

 

 

 

re模块

  re模块是用来做正则匹配。

  基本语法:

##代码:
import re

p=re.compile("^[0-9]")  #生成要匹配的正则对象 , ^代表从开头匹配,[0-9]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表匹配上了
m=p.match('132323Abcds')     #按上面生成的正则对象 去匹配 字符串, 如果能匹配成功,这个m就会有值, 否则m为None<br><br>if m: #不为空代表匹配上了
#m = p.match("^[0-9]",'14534Abc')   #简写(效果与上面两行一样)
print(m.group())    #m.group()返回匹配上的结果,此处为1,因为匹配上的是1这个字符<br>else:<br>  print("doesn't match.")<br>

##结果:
C:\Python35\python.exe D:/Python代码目录/day5/12.re模块.py
1

Process finished with exit code 0

"""
区别在于,第一种方式是提前对要匹配的格式进行了编译(对匹配公式进行解析),这样再去匹配的时候就不用在编译匹配的格式...
...第2种简写是每次匹配的时候 都 要进行一次匹配公式的编译,所以,如果你需要从一个5w行的文件中匹配出所有以数字开头的行...
...建议先把正则公式进行编译再匹配,这样速度会快点。
"""

  匹配格式:

  常用正则表达式符号:

'.'     #默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行
'^'     #匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r"^a","\nabc\neee",flags=re.MULTILINE)
'$'     #匹配字符结尾,或e.search("foo$","bfoo\nsdfsf",flags=re.MULTILINE).group()也可以
'*'     #匹配*号前的字符0次或多次,re.findall("ab*","cabb3abcbbac")  结果为['abb', 'ab', 'a']
'+'     #匹配前一个字符1次或多次,re.findall("ab+","ab+cd+abb+bba") 结果['ab', 'abb']
'?'     #匹配前一个字符1次或0次
'{m}'   #匹配前一个字符m次
'{n,m}' #匹配前一个字符n到m次,re.findall("ab{1,3}","abb abc abbcbbb") 结果'abb', 'ab', 'abb']
'|'     #匹配|左或|右的字符,re.search("abc|ABC","ABCBabcCD").group() 结果'ABC'
'(...)' #分组匹配,re.search("(abc){2}a(123|456)c", "abcabca456c").group() 结果 abcabca456c
 
 
'\A'    #只从字符开头匹配,re.search("\Aabc","alexabc") 是匹配不到的
'\Z'    #匹配字符结尾,同$
'\d'    #匹配数字0-9
'\D'    #匹配非数字
'\w'    #匹配[A-Za-z0-9]
'\W'    #匹配非[A-Za-z0-9]
's'     #匹配空白字符、\t、\n、\r , re.search("\s+","ab\tc1\n3").group() 结果 '\t'
 
'(?P<name>...)' #分组匹配 re.search("(?P<province>[0-9]{4})(?P<city>[0-9]{2})(?P<birthday>[0-9]{4})","371481199306143242").groupdict("city") 结果{'province': '3714', 'city': '81', 'birthday': '1993'}

   正则表达式常用的5种操作:

  1)re.match(pattern, string)     #从头匹配

>>> import re
>>> m = re.match("[a-z]", "aBc123")
>>> print(m)
<_sre.SRE_Match object; span=(0, 1), match='a'>

  2)re.search(pattern, string)    #匹配整个字符串,直到找到一个匹配

>>> import re
>>> m=re.search('hi','hello,hihihi')
>>> print(m)
<_sre.SRE_Match object; span=(6, 8), match='hi'>

  3)re.split()    # 将匹配到的格式当做分割点对字符串分割成列表

>>> import re
>>> m = re.split("[A-Z]", "abcD123E123")
>>> print(m)
['abc', '123', '123']
>>> 
>>> import re
>>> m = re.split("[1-9]", "abcD123E123")
>>> print(m)
['abcD', '', '', 'E', '', '', '']
>>> 
>>> import re
>>> m = re.split("[a-z]", "abcD123E123")
>>> print(m)
['', '', '', 'D123E123']
>>> 

  4)re.findall()    # 找到所有要匹配的字符并返回列表格式

>>> import re
>>> m = re.findall("[0-9]", "abcD123E123")
>>> print(m)
['1', '2', '3', '1', '2', '3']
>>> 
>>> import re
>>> m = re.findall("[a-z]", "abcD123E123")
>>> print(m)
['a', 'b', 'c']
>>> 
>>> import re
>>> m = re.findall("[A-Z]", "abcD123E123")
>>> print(m)
['D', 'E']
>>>

  5)re.sub(pattern, repl, string, count,flag)    # 替换匹配到的字符

>>> import re
>>> m = re.sub("[A-Z]","*", "abcD123E123",count=2)
>>> print(m)
abc*123*123
>>> 
>>> import re
>>> m = re.sub("[1-9]","*", "abcD123E123",count=4)
>>> print(m)
abcD***E*23
>>> 
>>> import re
>>> m = re.sub("[a-z]","*", "abcD123E123",count=1)
>>> print(m)
*bcD123E123
>>> 
##代码:
phone_str = "hey my name is wuchunwei, and my phone number is 18908651234, please call me if you are pretty!"
phone_str2 = "hey my name is yangmengmeng, and my phone number is 15101551234, please call me if you are pretty!"

m = re.search("(1)([358]\d{9})",phone_str)
m2 = re.search("(1)([358]\d{9})",phone_str2)
if m:
    print(m.group(),m2.group())

##结果:
C:\Python35\python.exe D:/Python代码目录/day5/12.re模块.py
18908651234 15101551234

Process finished with exit code 0
事例1:匹配手机号
##代码:
ip_addr = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255"

m = re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", ip_addr)

print(m.group())

##结果:
C:\Python35\python.exe D:/Python代码目录/day5/12.re模块.py
192.168.60.223

Process finished with exit code 0
事例2:匹配IP V4
##代码:
email = "chunwei.wu@126.com   http://www.caixin.com"

m = re.search(r"[0-9.a-z]{0,26}@[0-9.a-z]{0,20}.[0-9a-z]{0,8}", email)
print(m.group())

##结果:
C:\Python35\python.exe D:/Python代码目录/day5/12.re模块.py
chunwei.wu@126.com 

Process finished with exit code 0
事例3:匹配email

 

urllib模块

  urllib是用来处理URL的模块

  urllib.request;urllib.respone;urllib.robotparser;urllib.error

 

  urllib.request

from urllib import request
url="http://news.xinhuanet.com/politics/2017-07/30/c_1121403739.htm"


#urllib.request.urlopen(url[,data[,proxies]])#打开一个url的方法,返回一个文件对象,然后可以进行类似文件对象的操作#
'''
    urlopen返回对象提供方法:
        read() , readline() ,readlines() , fileno() , close() :这些方法的使用方式与文件对象完全一样
        info():返回一个httplib.HTTPMessage对象,表示远程服务器返回的头信息
        getcode():返回Http状态码。如果是http请求,200请求成功完成;404网址未找到
        geturl():返回请求的url
'''

a= request.urlopen(url) #返回文件对象
print("read()方法: ",a.read())    #a.read().decode()
print(a.info())
print(a.getcode())
print(a.geturl())


#urllib.urlretrieve(url[,filename[,reporthook[,data]]])方法将url定位到的html文件下载到你本地的硬盘中。如果不指定filename,则会存为临时文件#
'''

from urllib import request
url="http://news.xinhuanet.com/politics/2017-07/30/c_1121403739.htm"

a = request.urlretrieve(url)    #临时存放
print(type(a))
print(a[0]) #查看临时缓存位置C:\Users\ADMINI~1\AppData\Local\Temp\tmpcob_gc_c


b = request.urlretrieve(url,filename='123.txt') #通过filename指定存放位置(默认相对路径)
print(type(b))  #<class 'tuple'>
print(b[0]) #123.txt

'''


#urllib.urlcleanup() #
"""
清除由于urllib.urlretrieve()所产生的缓存
"""

 

traceback模块

  获取程序异常信息

import traceback

"""
traceback模块
    显示异常的内容更加详细
"""

try:
    int('esser')
except Exception as e:
    # print(e)
    
    '''
    invalid literal for int() with base 10: 'esser
    '''

print(traceback.format_exc()) ''' Traceback (most recent call last): File "D:/cmbd/autoclient/Tback.py", line 9, in <module> int('esser') ValueError: invalid literal for int() with base 10: 'esser' '''

 

   

posted on 2017-03-30 15:30  py_chunwei  阅读(293)  评论(0编辑  收藏  举报