python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】

时间:2023-03-09 21:48:41
python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】

一、shelve模块

  

 import shelve

 # 基于pickle模块,

 d = shelve.open('shelve_test')

 class Test(object):
def __init__(self, n):
self.n = n t1 = Test(123)
t2 = Test(456)
name = ['alex', 'rain', 'test']
d['test'] = name
d['t1'] = t1
d['t2'] = t2 d.close()

二、XML模块

  1.增、删、改、查

import xml.etree.ElementTree as ET

tree = ET.parse("list.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) # 修改
# 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')

  2.创建

 import xml.etree.ElementTree as ET

 # root
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 = ''
name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
age = ET.SubElement(name2, "age")
age.text = '' # 生成文档对象
et = ET.ElementTree(new_xml)
et.write("test.xml", encoding="utf-8", xml_declaration=True)

三、shutil模块

 import shutil
# http://www.cnblogs.com/wupeiqi/articles/4963027.html # copy fileobj
# f1 = open('access.log')
# f2 = open("access1.log", 'w')
# shutil.copyfileobj(f1, f2, length=1024) # copy file
# shutil.copyfile('access.log', 'access2.log') # 仅拷贝权限。内容、组、用户均不变
# shutil.copymode() # 拷贝状态的信息,包括:mode bits, atime, mtime, flags. 内容不变
# shutil.copystat() # copy 目录

四、subprocess模块

 import subprocess

 res = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd="/")
print(res.stdout.read()) # res.poll()
# res.terminate()
# res.wait() subprocess.getstatusoutput("ls")

五、RE模块

import re

# 从头匹配,很少使用
re.match("\d+", "")
# 匹配一次
re.search("\d+", "")
# 匹配多次
re.findall("\d+", "")
# 以逗号分割
re.split(",", "341,221")
# 匹配到进行替换,默认是替代所有,count指定次数.
re.sub("\d{4}", "", "1399,2017", count=1) # re.I (忽略大小写)
# print(re.search("[a-z]", "Alex", flags=re.I))
# re.M (匹配多行)
# print(re.search("^is", "my name\nis alex", flags=re.M))
# re.S (多行匹配在一起)
# print(re.search(".+", "my \nname", flags=re.S))

六、configparser模块

 mport configparser

 config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '',
'Compression': 'yes',
'CompressionLevel': ''} config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)

七、类

http://www.cnblogs.com/wupeiqi/p/4493506.html

http://www.cnblogs.com/wupeiqi/p/4766801.html