码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-03-02 19:45:32      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:ges   view   python   test   round   pytho   err   ica   tput   

一、shelve模块

  

 1 import shelve
 2 
 3 # 基于pickle模块,
 4 
 5 d = shelve.open(shelve_test)
 6 
 7 
 8 class Test(object):
 9     def __init__(self, n):
10         self.n = n
11 
12 t1 = Test(123)
13 t2 = Test(456)
14 name = [alex, rain, test]
15 d[test] = name
16 d[t1] = t1
17 d[t2] = t2
18 
19 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‘)
View Code

  2.创建

技术分享
 1 import xml.etree.ElementTree as ET
 2 
 3 # root
 4 new_xml = ET.Element("namelist")
 5 
 6 
 7 name = ET.SubElement(new_xml, "name", attrib={"enrolled": "yes"})
 8 age = ET.SubElement(name, "age", attrib={"checked": "no"})
 9 sex = ET.SubElement(name, "sex")
10 sex.text = 33
11 name2 = ET.SubElement(new_xml, "name", attrib={"enrolled": "no"})
12 age = ET.SubElement(name2, "age")
13 age.text = 19
14 
15 # 生成文档对象
16 et = ET.ElementTree(new_xml)
17 et.write("test.xml", encoding="utf-8", xml_declaration=True)
View Code

三、shutil模块

技术分享
 1 import shutil
 2 # http://www.cnblogs.com/wupeiqi/articles/4963027.html
 3 
 4 # copy fileobj
 5 # f1 = open(‘access.log‘)
 6 # f2 = open("access1.log", ‘w‘)
 7 # shutil.copyfileobj(f1, f2, length=1024)
 8 
 9 # copy file
10 # shutil.copyfile(‘access.log‘, ‘access2.log‘)
11 
12 # 仅拷贝权限。内容、组、用户均不变
13 # shutil.copymode()
14 
15 # 拷贝状态的信息,包括:mode bits, atime, mtime, flags. 内容不变
16 # shutil.copystat()
17 
18 # copy 目录
View Code

四、subprocess模块

技术分享
 1 import subprocess
 2 
 3 res = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd="/")
 4 print(res.stdout.read())
 5 
 6 # res.poll()
 7 # res.terminate()
 8 # res.wait()
 9 
10 subprocess.getstatusoutput("ls")
View Code

五、RE模块

技术分享
import re


# 从头匹配,很少使用
re.match("\d+", "341221")
# 匹配一次
re.search("\d+", "341221")
# 匹配多次
re.findall("\d+", "341221")
# 以逗号分割
re.split(",", "341,221")
# 匹配到进行替换,默认是替代所有,count指定次数.
re.sub("\d{4}", "1995", "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))
View Code

六、configparser模块

 1 mport configparser
 2  
 3 config = configparser.ConfigParser()
 4 config["DEFAULT"] = {ServerAliveInterval: 45,
 5                       Compression: yes,
 6                      CompressionLevel: 9}
 7  
 8 config[bitbucket.org] = {}
 9 config[bitbucket.org][User] = hg
10 config[topsecret.server.com] = {}
11 topsecret = config[topsecret.server.com]
12 topsecret[Host Port] = 50022     # mutates the parser
13 topsecret[ForwardX11] = no  # same here
14 config[DEFAULT][ForwardX11] = yes
15 with open(example.ini, w) as configfile:
16    config.write(configfile)

七、类

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

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

 

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

标签:ges   view   python   test   round   pytho   err   ica   tput   

原文地址:http://www.cnblogs.com/weibiao/p/6491950.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!