Python基础(七)-模块概念及常用模块

一、模块介绍

1.1、模块定义

模块就是一组功能的集合体,我们的程序可以导入模块来复用模块里的功能

在Python中,一个.py文件就称之为一个模块(Module)

一个模块就是一个包含了一组功能的python文件,比如spam.py,模块名为spam,可以通过import spam使用。

1.2、模块分类

1)python标准库

2)第三方模块

3)应用程序自定义模块

1.3、模块的导入

1.3.1、import导入

解释器有自己的搜索路径,存在sys.path里。在当前目录下存在与要引入模块同名的文件,就会把要引入的模块屏蔽掉  

import 模块名   #导入单个模块
import sys,os,re  #导入多个模块

可以模块名起别名

import spam as sm
-----------------------------------------------------------

#有两中sql模块mysql和oracle,根据用户的输入,选择不同的sql功能
#mysql.py
def sqlparse():
    print('from mysql sqlparse')

#oracle.py
def sqlparse():
    print('from oracle sqlparse')

#test.py
db_type=input('>>: ')
if db_type == 'mysql':
    import mysql as db
elif db_type == 'oracle':
    import oracle as db

db.sqlparse() 
-----------------------------------------------------------

#假设有两个模块xmlreader.py和csvreader.py,它们都定义了函数read_data(filename):用来从文件中读取一些数据,但采用不同的输入格式。可以编写代码来选择性地挑选读取模块

if file_format == 'xml':
    import xmlreader as reader
elif file_format == 'csv':
    import csvreader as reader
data=reader.read_date(filename)

1.3.2、from … import..导入

from spam import 模块名
from spam import 模块名 as 别名
from spam import *  #不推荐

使用from...import...则是将spam中的名字直接导入到当前的名称空间中,所以在当前名称空间中,直接使用名字就可以了、无需加前缀:spam.

二、包的概念

2.1、包的介绍

为了避免模块名冲突,Python又引入了按目录来组织模块的方法,称为包(Package)。

每一个包目录下面都会有一个__init__.py的文件,这个文件是必须存在的,否则,Python就把这个目录当成普通目录(文件夹),而不是一个包。__init__.py可以是空文件,也可以有Python代码,因为__init__.py本身就是一个模块,而它的模块名就是对应包的名字

调用包就是执行包下的__init__.py文件

2.2、包使用注意事项

1

在nod1里import  hello是找不到的,pycharm把myapp路径加入到了sys.path,但是程序一旦在命令行运行就报错,需要自己将路径加进去

import sys,os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
import hello
hello.hello1()

注意

1)如果我们是直接执行某个.py文件的时候,该文件中那么”__name__ == '__main__'“是True

2)我们如果从另外一个.py文件通过import导入该文件的时候,这时__name__的值就是我们这个py文件的名字而不是__main__。

3)调试代码的时候,在”if __name__ == '__main__'“中加入一些我们的调试代码,我们可以让外部模块调用的时候不执行我们的调试代码,但是如果我们想排查问题的时候,直接执行该模块文件,调试代码能够正常运行

三、常用模块

3.1、time与datetime模块

3.1.1、time模块

表示时间的方式

  1. 时间戳(timestamp):从1970年1月1日00:00:00开始按秒计算的偏移量
  2. 格式化的时间字符串(Format String)
  3. 结构化的时间(struct_time):struct_time元组共有9个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)

image

import time
#将时间戳时间转换为结构化时间
print(time.localtime()) #time.struct_time(tm_year=2019, tm_mon=9, tm_mday=6, tm_hour=13, tm_min=30, tm_sec=55, tm_wday=4, tm_yday=249, tm_isdst=0)
print(time.localtime(1473525444.037215)) #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0, tm_min=37, tm_sec=24, tm_wday=6, tm_yday=255, tm_isdst=0)
print(time.gmtime()) #time.struct_time(tm_year=2019, tm_mon=9, tm_mday=6, tm_hour=5, tm_min=33, tm_sec=56, tm_wday=4, tm_yday=249, tm_isdst=0)

#将结构化时间转换为时间戳
print(time.mktime(time.localtime()))  #1567748196.0

#将结构化时间转换为格式化的时间字符串
print(time.strftime("%Y-%m-%d %X",time.localtime()))  #2019-09-06 13:39:42

#把一个格式化时间字符串转化为struct_time,与strftime()是逆操作
print(time.strptime("2019-09-06 13:39:42","%Y-%m-%d %X"))  #time.struct_time(tm_year=2019, tm_mon=9, tm_mday=6, tm_hour=13, tm_min=39, tm_sec=42, tm_wday=4, tm_yday=249, tm_isdst=-1)

image

print(time.asctime())  #Fri Sep  6 13:46:08 2019
print(time.ctime())    #Fri Sep  6 13:46:59 2019
print(time.time())     #1567748858.9732707
print(time.ctime(time.time()))  #Fri Sep  6 13:47:21 2019

时间操作总结:

import time
 
# 1 time() :返回当前时间的时间戳
time.time()  #1473525444.037215
 
#----------------------------------------------------------
 
# 2 localtime([secs])
# 将一个时间戳转换为当前时区的struct_time。secs参数未提供,则以当前时间为准。
time.localtime() #time.struct_time(tm_year=2016, tm_mon=9, tm_mday=11, tm_hour=0,tm_min=38, tm_sec=39,tm_wday=6, tm_yday=255, tm_isdst=0)
time.localtime(1473525444.037215)
 
#----------------------------------------------------------
 
# 3 gmtime([secs]) 和localtime()方法类似,gmtime()方法是将一个时间戳转换为UTC时区(0时区)的struct_time。
 
#----------------------------------------------------------
 
# 4 mktime(t) : 将一个struct_time转化为时间戳。
print(time.mktime(time.localtime()))#1473525749.0
 
#----------------------------------------------------------
 
# 5 asctime([t]) : 把一个表示时间的元组或者struct_time表示为这种形式:'Sun Jun 20 23:21:05 1993'。
# 如果没有参数,将会将time.localtime()作为参数传入。
print(time.asctime())#Sun Sep 11 00:43:43 2016
 
#----------------------------------------------------------
 
# 6 ctime([secs]) : 把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式。如果参数未给或者为
# None的时候,将会默认time.time()为参数。它的作用相当于time.asctime(time.localtime(secs))。
print(time.ctime())  # Sun Sep 11 00:46:38 2016
 
print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016
 
# 7 strftime(format[, t]) : 把一个代表时间的元组或者struct_time(如由time.localtime()和
# time.gmtime()返回)转化为格式化的时间字符串。如果t未指定,将传入time.localtime()。如果元组中任何一个
# 元素越界,ValueError的错误将会被抛出。
print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56
 
# 8 time.strptime(string[, format])
# 把一个格式化时间字符串转化为struct_time。实际上它和strftime()是逆操作。
print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
 
#time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
#  tm_wday=3, tm_yday=125, tm_isdst=-1)
 
#在这个函数中,format默认为:"%a %b %d %H:%M:%S %Y"。
 
 
# 9 sleep(secs)
# 线程推迟指定的时间运行,单位为秒。
 
# 10 clock()
# 这个需要注意,在不同的系统上含义不同。在UNIX系统上,它返回的是“进程时间”,它是用秒表示的浮点数(时间戳)。
# 而在WINDOWS中,第一次调用,返回的是进程运行的实际时间。而第二次之后的调用是自第一次调用以后到现在的运行
# 时间,即两次时间差。

3.1.2、datetime模块

import datetime

print(datetime.datetime.now())  #2019-09-06 14:09:04.430077
# print(datetime.date.fromtimestamp(time.time()))
print(datetime.datetime.now() + datetime.timedelta(3))  #2019-09-09 14:10:02.750837
print(datetime.datetime.now() + datetime.timedelta(-3)) #2019-09-03 14:10:21.448636
print(datetime.datetime.now() + datetime.timedelta(hours=3))  #2019-09-06 17:10:37.330027
print(datetime.datetime.now() + datetime.timedelta(minutes=30))  #2019-09-06 14:41:02.646481

c_time  = datetime.datetime.now()  #2019-09-06 14:41:23.021579
print(c_time.replace(minute=3,hour=2)) #时间替换 2019-09-06 02:03:23.021579

3.2、random模块

import random
print(random.random())   #(0,1)float
print(random.randint(1,3))  #[1,3]整数
print(random.randrange(1,3))  #[1,3)整数
print(random.choice([1,"a",[4,5],"bc"]))  #列表中随机取一个
print(random.sample([1,"a",[4,5],"bc"],2))  #列表中任意两个组合
print(random.uniform(1,3))  ##大于1小于3的小数,如1.927109612082716

#打乱顺序
item=[1,3,5,7,9]
print(random.shuffle(item))  #None,调用 方式不对
random.shuffle(item)
print(item)  #[3, 5, 7, 1, 9]

获取随机验证码:

import random
def make_code(n):
    res=''
    for i in range(n):
        s1=chr(random.randint(65,90))
        s2=str(random.randint(0,9))
        res+=random.choice([s1,s2])
    return res

print(make_code(9))

3.3、os模块

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

os.getcwd()                        #获取当前工作目录,即当前python脚本工作的目录路径
os.chdir("dirname")                #改变当前脚本工作目录;相当于shell下cd
os.curdir                          #返回当前目录: ('.')
os.pardir                          #获取当前目录的父目录字符串名:('..')
os.makedirs('dirname1/dirname2')   #可生成多层递归目录
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.sep                             #输出操作系统特定的路径分隔符,win下为"\",Linux下为"/"
os.linesep                         #输出当前平台使用的行终止符,win下为"
",Linux下为"
"
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.getsize(path)                #返回path的大小

3.4、sys模块

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

进度条:

#=========知识储备==========
#进度条的效果
[#             ]
[##            ]
[###           ]
[####          ]

#指定宽度
print('[%-15s]' %'#')
print('[%-15s]' %'##')
print('[%-15s]' %'###')
print('[%-15s]' %'####')

#打印%
print('%s%%' %(100)) #第二个%号代表取消第一个%的特殊意义

#可传参来控制宽度
print('[%%-%ds]' %50) #[%-50s]
print(('[%%-%ds]' %50) %'#')
print(('[%%-%ds]' %50) %'##')
print(('[%%-%ds]' %50) %'###')


#=========实现打印进度条函数==========
import sys
import time

def progress(percent,width=50):
    if percent >= 1:
        percent=1
    show_str=('[%%-%ds]' %width) %(int(width*percent)*'#')
    print('
%s %d%%' %(show_str,int(100*percent)),file=sys.stdout,flush=True,end='')


#=========应用==========
data_size=1025
recv_size=0
while recv_size < data_size:
    time.sleep(0.1) #模拟数据的传输延迟
    recv_size+=1024 #每次收1024

    percent=recv_size/data_size #接收的比例
    progress(percent,width=70) #进度条的宽度70

3.5、json模块

1)为什么要用json模块?

eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特殊类型的时候,eval就不管用了,所以eval的重点还是通常用来执行一个字符串表达式,并返回表达式的值

import json
x="[null,true,false,1]"
print(eval(x)) #报错,无法解析null类型,而json就可以
print(json.loads(x))  #[None, True, False, 1]

2)序列化

序列化:把对象(变量)从内存中变成可存储或传输的过程

  • 持久保存状态
  • 跨平台数据交互

3)json模块的使用

import json
dic = {'name': 'alvin', 'age': 23, 'sex': 'male'}
print(type(dic))  # <class 'dict'>

#序列化
j = json.dumps(dic)
print(type(j))  # <class 'str'>
# dic={'name':'alex'}  #---->{"name":"alex"}----->'{"name":"alex"}'
# i=8                  #---->'8'
# s='hello'            #---->"hello"------>'"hello"'
# l=[11,22]            #---->"[11,22]"

f = open('序列化对象', 'w')    #{"sex": "male", "name": "alvin", "age": 23}
f.write(j)  #等价于json.dump(dic,f)
f.close()
#=====================================================================
#反序列化
import json

f = open('序列化对象')
data = json.loads(f.read())  # 等价于data=json.load(f)
print(data)  #{'sex': 'male', 'age': 23, 'name': 'alvin'}

3.6、pickle模块

pickel只能用于python

##序列化
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'])  #23

3.7、shelve模块

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

import shelve
f = shelve.open(r'shelve.txt')

# f['stu1_info']={'name':'alex','age':'18'}
# f['stu2_info']={'name':'alvin','age':'20'}
# f['school_info']={'website':'oldboyedu.com','city':'beijing'}
#
#
# f.close()

print(f.get('stu1_info')['age'])  #18

image

3.8、xml模块

xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,json使用起来更简单,但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("xml_test.xml")
root = tree.getroot()
print(root.tag)  #data

# 遍历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)
# year 2008
# year 2011
# year 2011
# # ---------------------------------------

# 修改
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"})
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)  # 打印生成的格式

生成的结果:

<?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>

3.9、re模块

正则表达式(或 RE)是一种小型的、高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现

3.9.1、普通字符匹配

>>> import re
>>> re.findall("abc","jdkajkabcodojal")
['abc']

3.9.2、元字符匹配

元字符:. ^ $ * + ? { } [ ] | ( )

1)元字符之. ^ $ * + ? { }

>>> import re
>>> re.findall('a..in','helloalvin')
['alvin']
>>> re.findall('^a...n','alvinhelloawwwn')
['alvin']
>>> re.findall('a...n$','alvinhelloawwwn')
['awwwn']
>>> re.findall('abc*','abcccc')  #贪婪匹配[0,+oo] 
['abcccc']
>>> re.findall('abc+','abccc')  #[1,+oo]
['abccc']
>>> re.findall('abc?','abccc')  #[0,1]
['abc']
>>> re.findall('abc{1,4}','abccc')
['abccc']
>>>

#前面的*,+,?等都是贪婪匹配,也就是尽可能匹配,后面加?号使其变成惰性匹配
>>> re.findall('abc*?','abcccccc')   #取消贪婪匹配
['ab']

2)元字符[]

>>> import re
>>> re.findall('a[bc]d','acd')
['acd']
>>> re.findall('[a-z]','acd')
['a', 'c', 'd']
>>> re.findall('[.*+]','a.cd+')  #[]中不代表特别的含义
['.', '+']

#在字符集里有功能的符号: - ^ 
>>> re.findall('[1-9]','45dha3')
['4', '5', '3']
>>> re.findall('[^ab]','45bdha3')
['4', '5', 'd', 'h', '3']
>>> re.findall('[d]','45bdha3')
['4', '5', '3']

3)元字符之转义符

反斜杠后边跟元字符去除特殊功能,比如.
反斜杠后边跟普通字符实现特殊功能,比如d

d  #匹配任何十进制数;它相当于类 [0-9]。
D  #匹配任何非数字字符;它相当于类 [^0-9]。
s  #匹配任何空白字符;它相当于类 [ 	

fv]。
S  #匹配任何非空白字符;它相当于类 [^ 	

fv]。
w  #匹配任何字母数字字符;它相当于类 [a-zA-Z0-9_]。
W  #匹配任何非字母数字字符;它相当于类 [^a-zA-Z0-9_]
  #匹配一个特殊字符边界,比如空格 ,&,#等

示例:

>>> import re
>>> re.findall('I','I am LIST')
[]
>>> re.findall(r'I','I am LIST')
['I']

>>> re.findall('c\\l','abcle')
['c\l']
>>> re.findall(r'c\l','abcle')
['c\l']

image

4)元字符之分组

>>> re.findall(r'(ad)+', 'add')
['ad']
>>> re.search('(?P<id>d{2})/(?P<name>w{3})','23/com')
<re.Match object; span=(0, 6), match='23/com'>
>>> re.search('(?P<id>d{2})/(?P<name>w{3})','23/com').group()
'23/com'
>>> re.search('(?P<id>d{2})/(?P<name>w{3})','23/com').group('id')
'23'

5)元字符之|

>>> re.search('(ab)|d','rabhdg8sd')
<re.Match object; span=(1, 3), match='ab'>
>>> re.search('(ab)|d','rabhdg8sd').group()
'ab'

6)re模块下常用方法

>>> import re

>>> re.findall('a','alvin yuan') #返回所有满足匹配条件的结果,放在列表里
['a', 'a']

>>> re.search('a','alvin yuan'通过调用group()方法得到匹配的字符串,如果字符串没有匹配,则返回None。).group()  ##函数会在字符串内查找模式匹配,只到找到第一个匹配然后返回一个包含匹配信息的对象,该对象可以
'a'

>>> re.match('a','abc').group()  #同search,不过尽在字符串开始处进行匹配
'a'

>>> re.split('[ab]','abcd')  ##先按'a'分割得到''和'bcd',在对''和'bcd'分别按'b'分割
['', '', 'cd']

>>> re.sub('d','abc','alvin5yuan6',1)
'alvinabcyuan6'
>>> re.subn('d','abc','alvin5yuan6')  #得到替换的字符串,并输出替换了多少次
('alvinabcyuanabc', 2)

>>> obj=re.compile('d{3}')   #编译
>>> ret=obj.search('abc123eeee')
>>> print(ret.group())#123
123

>>> import re
>>> ret=re.finditer('d','ds3sy4784a')
>>> print(ret)
<callable_iterator object at 0x000001D731B6A2E8>   #迭代器
>>> print(next(ret).group())
3
>>> print(next(ret).group())
4

注意补充

>>> re.findall('www.(baidu|oldboy).com','www.oldboy.com')
['oldboy']    #这是因为findall会优先把匹配结果组里内容返回,如果想要匹配结果,取消权限即可
>>> re.findall('www.(?:baidu|oldboy).com','www.oldboy.com')
['www.oldboy.com']

>>> print(re.findall("<(?P<tag_name>w+)>w+</(?P=tag_name)>","<h1>hello</h1>"))
['h1']
>>> print(re.search(r"<(w+)>w+</1>","<h1>hello</h1>").group())
<h1>hello</h1>

#匹配出所有的整数
import re
# ret=re.findall(r"d+{0}]","1-2*(60+(-40.35/5)-(-4*3))")
ret=re.findall(r"-?d+.d*|(-?d+)","1-2*(60+(-40.35/5)-(-4*3))")
ret.remove("")  
print(ret)  #['1', '-2', '60', '5', '-4', '3']

3.10、logging模块

3.10.1、根据logging.basicConfig设置日志

import logging

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='logging.log',
                    filemode='w')

logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

#输出
Sat, 07 Sep 2019 08:45:43 logging-module.py[line:18] DEBUG debug message
Sat, 07 Sep 2019 08:45:43 logging-module.py[line:19] INFO info message
Sat, 07 Sep 2019 08:45:43 logging-module.py[line:20] WARNING warning message
Sat, 07 Sep 2019 08:45:43 logging-module.py[line:21] ERROR error message
Sat, 07 Sep 2019 08:45:43 logging-module.py[line:22] CRITICAL critical message

logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有:

filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
format:指定handler使用的日志显示格式。
datefmt:指定日期时间格式。
level:设置rootlogger(后边会讲解具体概念)的日志级别
stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件(f=open('test.log','w')),默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

#format参数中可能用到的格式化串:
%(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用户输出的消息

3.10.2、根据logger对象设置

https://www.cnblogs.com/yuanchenqi/articles/5732581.html

import logging

# 创建一个logger
logger = logging.getLogger()

logger1 = logging.getLogger('mylogger')
logger1.setLevel(logging.DEBUG)

logger2 = logging.getLogger('mylogger')
logger2.setLevel(logging.INFO)

logger3 = logging.getLogger('mylogger.child1')
logger3.setLevel(logging.WARNING)

logger4 = logging.getLogger('mylogger.child1.child2')
logger4.setLevel(logging.DEBUG)

logger5 = logging.getLogger('mylogger.child1.child2.child3')
logger5.setLevel(logging.DEBUG)

# 创建一个handler,用于写入日志文件
fh = logging.FileHandler('test.log')

# 再创建一个handler,用于输出到控制台
ch = logging.StreamHandler()

# 定义handler的输出格式formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)

# 定义一个filter
# filter = logging.Filter('mylogger.child1.child2')
# fh.addFilter(filter)

# 给logger添加handler
# logger.addFilter(filter)
logger.addHandler(fh)
logger.addHandler(ch)

# logger1.addFilter(filter)
logger1.addHandler(fh)
logger1.addHandler(ch)

logger2.addHandler(fh)
logger2.addHandler(ch)

# logger3.addFilter(filter)
logger3.addHandler(fh)
logger3.addHandler(ch)

# logger4.addFilter(filter)
logger4.addHandler(fh)
logger4.addHandler(ch)

logger5.addHandler(fh)
logger5.addHandler(ch)

# 记录一条日志
logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')

logger1.debug('logger1 debug message')
logger1.info('logger1 info message')
logger1.warning('logger1 warning message')
logger1.error('logger1 error message')
logger1.critical('logger1 critical message')

logger2.debug('logger2 debug message')
logger2.info('logger2 info message')
logger2.warning('logger2 warning message')
logger2.error('logger2 error message')
logger2.critical('logger2 critical message')

logger3.debug('logger3 debug message')
logger3.info('logger3 info message')
logger3.warning('logger3 warning message')
logger3.error('logger3 error message')
logger3.critical('logger3 critical message')

logger4.debug('logger4 debug message')
logger4.info('logger4 info message')
logger4.warning('logger4 warning message')
logger4.error('logger4 error message')
logger4.critical('logger4 critical message')

logger5.debug('logger5 debug message')
logger5.info('logger5 info message')
logger5.warning('logger5 warning message')
logger5.error('logger5 error message')
logger5.critical('logger5 critical message')

3.11、configparser模块

文档格式如下:

[DEFAULT]
ServerAliveInterval = 45
Compression = yes
CompressionLevel = 9
ForwardX11 = yes
  
[bitbucket.org]
User = hg
  
[topsecret.server.com]
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'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here

config['DEFAULT']['ForwardX11'] = 'yes'

with open('example.ini', 'w') as configfile:
    config.write(configfile)   #将配置写到文件中
增删改查:
import configparser
config = configparser.ConfigParser()

#==============================查====================================#
print(config.sections())   #[],此时还没有关联文件

config.read('example.ini')  #读取文件配置

print(config.sections())   #['bitbucket.org', 'topsecret.server.com'],默认不会读取[default]的配置

print('bytebong.com' in config)# False

print(config['bitbucket.org']['User']) # hg

print(config['DEFAULT']['Compression']) #yes

print(config['topsecret.server.com']['ForwardX11'])  #no

for key in config['bitbucket.org']:
    print(key)   #还会默认读取[DEFAULT]

# user
# serveraliveinterval
# compression
# compressionlevel
# forwardx11

#读取键值
print(config.options('bitbucket.org'))#['user', 'serveraliveinterval', 'compression', 'compressionlevel', 'forwardx11']

#读取键值对
print(config.items('bitbucket.org'))  #[('serveraliveinterval', '45'), ('compression', 'yes'), ('compressionlevel', '9'), ('forwardx11', 'yes'), ('user', 'hg')]

print(config.get('bitbucket.org','compression'))#yes

#==========================删,改,增==================================#
config.add_section('yuan')  #添加section

config.remove_section('topsecret.server.com')  #删除section
config.remove_option('bitbucket.org','user')  #删除键值对

config.set('bitbucket.org','k1','11111')  #设置键值

config.write(open('i.cfg', "w"))  #将修改的配置写道文件

3.12、hashlib模块

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

import hashlib

m = hashlib.md5()  # m=hashlib.sha256()
m.update('hello'.encode('utf8'))
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,与上面相同

以上加密算法虽然依然非常厉害,但时候存在缺陷,即:通过撞库可以反解。所以,有必要对加密算法中添加自定义key(加盐)再来做加密。

import hashlib

hash = hashlib.sha256('898oaFs09f'.encode('utf8')) #盐898oaFs09f
hash.update('alvin'.encode('utf8'))
print(hash.hexdigest())  # e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7
python 还有一个 hmac 模块,它内部对我们创建 key 和 内容 再进行处理然后再加密:
import hmac
h = hmac.new('alvin'.encode('utf8'))
h.update('hello'.encode('utf8'))
print (h.hexdigest())#320df9832eab4c038b6c1d7ed73a5940
原文地址:https://www.cnblogs.com/hujinzhong/p/11474205.html