一、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

python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】的更多相关文章

  1. 函数和常用模块【day06】:shelve模块(五)

    本节内容 1.简述 2.shelve概念 3.shelve模块使用 4.总结 一.简述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,不能dum ...

  2. Python time、datetime、os、random、sys、hashlib、json、shutil、logging、paramiko、subprocess、ConfigParser、xml、shelve模块的使用

    文章目录: 1. time & datetime模块 2. os模块 3. random模块 4. sys模块 5. hashlib模块 6. json模块 7. shutil模块 8. lo ...

  3. Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解

    把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...

  4. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

  5. Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)

    目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...

  6. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  7. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  8. Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块

    Python第十一天    异常处理  glob模块和shlex模块    打开外部程序和subprocess模块  subprocess类  Pipe管道  operator模块   sorted函 ...

  9. python_day7【模块configparser、XML、requests、shutil、系统命令-面向对象】之篇

    python内置模块补充 一.configparser configparser:用户处理特定格式的文件,其本质是利用open打开文件 # 节点 [section1] #键值对k1 = v1 k2:v ...

随机推荐

  1. Python List 中 Append 和 Extent 方法不返回值。

    Append: list的append 方法是没有返回值的,因此list 的引用L被置为空 结果也就自然是空了. 再看一个例子: append 跟extend方法的区别是什么呢? 其实这样看的就很清楚 ...

  2. ECMall 中URL体系的改造思路

    EC系列的产品都已停止更新很久了,但其对中国中小电商企业的影响无疑是巨大的.很多公司,都是直接拿来即改,改了即用. 但他们都有个问题,代码是比较传统的开发模式过来的,尤其ecshop.ECMall系统 ...

  3. 2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 二分+贪心

    /** 题目:2016-2017 ACM-ICPC CHINA-Final Ice Cream Tower 链接:http://codeforces.com/gym/101194 题意:给n个木块,堆 ...

  4. pom打包参数选择

    pom.xml配置 <profiles> <profile> <id>dev</id> <properties> <token> ...

  5. 控件禁用与启easyui用

    1.validatebox可以用的用法:前两种适用于单个的validatebox;第三种应用于整个form里面的输入框; <1>.$("#id").attr(" ...

  6. Django 最佳实践

    不错的Django实践规范,转自Github: https://github.com/brantyoung/zh-django-best-practices/blob/master/readme.rs ...

  7. wpf ComboBox设置默认值

    最新的wpf的ComboBox设置默认值得方法是,给VM中的数据集合第一个元素插入一个提示项目,比如:请选择一项,然后通过数据绑定可以实现默认选中第一项,下面我就贴一下示例代码: xaml页面: &l ...

  8. Chem 3D中怎么创建立体模型

    ChemDraw作为一款很受大家欢迎的化学绘图软件,其在绘制平面化学方面的功能已经非常的强大了,其实它也可以绘制3D图形.Chem 3D就是绘制3D图形的重要组件.而且为了满足不同的用户绘图的需求,可 ...

  9. 利用python输出000至999中间的数

    打造一个000-999的字典 ): : i = '+str(i) elif <=i and i<: i ='+str(i) else: i=str(i) with open('1.txt' ...

  10. [转]python-元类

    转载于:刘羽冲 两句话掌握python最难知识点——元类 千万不要被所谓“元类是99%的python程序员不会用到的特性”这类的说辞吓住.因为每个中国人,都是天生的元类使用者 学懂元类,你只需要知道两 ...