day6-面向对象、常用模块使用2

学习内容:

1. 常用模块

2. 面向对象

 

1. 常用模块:

shutil

shelve

xml

pyYAML

configparser

hashlib

subprocess

re

 

shutil:文件复制、压缩、解压缩处理模块。

# shutil 文件复制、压缩、解压缩处理模块
import shutil

# copy文件对象,相当于"cp test.txt test2.txt"
f = open("test.txt")
f2 = open("test2.txt","w")

shutil.copyfileobj(f,f2)

# 直接copy文件名
shutil.copyfile("test.txt","test3.txt")

# copy文件和权限,目标可以是文件也可以是目录
shutil.copy("test.txt","test_dir")

# copy文件和状态信息
shutil.copy2("test.txt","test_dir")

# 仅copy权限,内容、组、用户均不变
shutil.copymode("test.txt","test3.txt")

# 仅copy状态信息,包括:mode bits,atime,mtime,flags
shutil.copystat("test.txt","test3.txt")

# copy目录(可以忽略文件)
shutil.copytree("d:\PyCode\day5","d:\PyCode\day6\Test_dir2",ignore=shutil.ignore_patterns("atm","*.txt","*.log"))

# 删除目录
shutil.rmtree("d:\PyCode\day6\Test_dir2")

# 移动目录d:\PyCode\day6\Test_dir2到d:\PyCode\day5
shutil.move("d:\PyCode\day6\Test_dir2","d:\PyCode\day5")

# 压缩目录:d:\PyCode\day5为day.zip
shutil.make_archive("day5","zip","d:\PyCode\day5")

# 解压文件day5.zip到目录test_dir
shutil.unpack_archive("day5.zip","test_dir")

# zipfile用法
import zipfile

#  打包文件test.txt test2.txt logging.txt day5.zip到压缩文件ziptest.zip
z = zipfile.ZipFile("ziptest.zip","w")
z.write("test.txt")
z.write("test2.txt")
z.write("d:\PyCode\day5\logging.txt",arcname="logging.txt")
z.write("day5.zip")
z.close()

# 解压ziptest.zip里的子文件logging.txt到Test_dir2
z = zipfile.ZipFile("ziptest.zip","r")
z.extract("logging.txt",path="Test_dir2")

# 解压所有文件到指定的目录
z.extractall(path="c:\Test_dir2")

# tarfile的用法
import tarfile
# pack
tar = tarfile.open("tar-test.tar","w")
tar.add("test.txt")
tar.add("test2.txt")
tar.add("d:\PyCode\day5\logging.txt",arcname="logging.txt")
tar.close()

# unpack
tar = tarfile.open("tar-test.tar","r")
tar.extractall(path="c:\jbc")
tar.close()
View Code

 

shelve: 序列化操作模块。

import shelve
d = shelve.open('shelve_test')

name = ["jerry","tom","wills"]
info = {
    "001":"jerry",
    "002":"tom"
}

d["k1"] = name # 持久化列表
d["k2"] = info # 持久化字典
d.close()

d = shelve.open('shelve_test')
print(d["k1"])
print(d["k1"][0])
print(d["k2"])
print(d["k2"]["001"])

输出:
['jerry', 'tom', 'wills']
jerry
{'001': 'jerry', '002': 'tom'}
jerry
View Code

 

xml:xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单。

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>
View Code
import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.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)
输出:
country {'name': 'Liechtenstein'}
rank 2
year 2008
gdppc 141100
neighbor None
neighbor None
country {'name': 'Singapore'}
rank 5
year 2011
gdppc 59900
neighbor None
country {'name': 'Panama'}
rank 69
year 2011
gdppc 13600
neighbor None
neighbor None


# 遍历year
for i in root.iter('year'):
    print(i.tag,i.text)

输出:
year 2008
year 2011
year 2011
View Code

 

修改和删除:

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')
View Code

 

pyYAML:处理ymal格式的文档

import yaml

names = {'version': '2', 'services': 'nginx'}

# 加载文件至Python字典
f = open("yamltest.yaml")
data = yaml.load(f)
f.close()
print(data)

# 序列化Python字典至文件
f = open("yamltest2.yaml","w")
yaml.dump(names,f)
f.close()
View Code

 

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

创建自定义配置文件:

import configparser

config = configparser.ConfigParser()

config["mysqld"] = {
    "port":"3306",
    "host":"localhost",
    "username":"root",
    "password":"pass"
}

config["mysqldump"] = {}
config["mysqldump"]["max_allowed_packet"] = "16M"

config['mysqld_safe'] = {}
safe = config['mysqld_safe']
safe["pid-file"] = "/data/mysql/mysql.pid"
safe["username"] = "test01"
safe["password"] = "test123456"

with open("my.cnf","w")  as cnf:
    config.write(cnf)

输出:
cat my.cnf
[mysqld]
port = 3306
host = localhost
username = root
password = pass

[mysqldump]
max_allowed_packet = 16M

[mysqld_safe]
pid-file = /data/mysql/mysql.pid
username = test01
password = test123456
View Code

 

读取配置文件:

>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config.read("my.cnf")
['my.cnf']
>>> config.sections()
['mysqld', 'mysqldump', 'mysqld_safe']
>>> 'mysqld' in config
True
>>> 'mysqld2' in config
False
>>> config["mysqld"]["port"]
'3306'
>>> for key in config["mysqld"]: print(key)
...
port
host
username
password
>>> for key in config["mysqld"]: print(key,config["mysqld"][key])
...
port 3306
host localhost
username root
password pass
View Code

 

增删改查:

import configparser
config = configparser.ConfigParser()

config.read("my.cnf")

# 增加section: [mysqld] 下的socket选项
config["mysqld"]["socket"] = "/tmp/mysql.sock"

# 增加一个新的section: [xxx]
config.add_section("xxx")

# 删除section: [mysqld_safe]
config.remove_section("mysqld_safe")

# 删除section: [mysqld]下的socket选项
config.remove_option("mysqld","socket")

# 修改
config.set("mysqld","port","3666")

# 将变更写入文件
with open("my.cnf","w") as cnf:
    config.write(cnf)
View Code

 

hashlib:用于产生md5 sha算法的hash值。

import hashlib

# 产生字符串的hash值
msg = hashlib.md5()
msg.update(b"asdf;lkasfl;ajsd;lfjas;dfja;sldfj")
print(msg.hexdigest())

# 产生文件的hash值
f = open("my.cnf")
msg2 = hashlib.md5()
for line in f.read():
    line = line.encode("utf-8")
    msg2.update(line)
print(msg2.hexdigest())

输出:
0e62a6388048088d300c01822a239af2
c95bd53b39f2bc82124f3a9680c54a25
View Code

 

subprocess:  

pass

 

re: 正则表达式

常用正则表达式符号:

'.'     默认匹配除\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'}
View Code

 

最常用的匹配语法:

re.match 从头开始匹配
re.search 匹配包含
re.findall 把所有匹配到的字符放到以列表中的元素返回
re.splitall 以匹配到的字符当做列表分隔符
re.sub      匹配字符并替换 

 

posted @ 2017-06-26 15:03  不露自威  阅读(143)  评论(0编辑  收藏  举报