一、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. Centos + HHVM 生产环境安装!

    一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop fi ...

  2. Java中的BlockingQueue小结

    BlockingQueue是java.util.concurrent下的主要用来控制线程同步的工具. 主要的方法是:put.take一对阻塞存取:add.poll一对非阻塞存取. 插入: 1) add ...

  3. textarea去掉边框

    <textarea style="BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; BORDER-RIGHT: 0px solid; ...

  4. PHP中钩子函数的实现与认识

    PHP中钩子函数的实现与认识 分类:PHP编程  作者:rming  时间:2014-09-21 假如有这么一段程序: function fun(){ fun1(); fun2(); }   首先程序 ...

  5. [翻译]在ASP.NET Web API中通过OData支持查询和分页

    OData可以通过形如http://localhost/Products?$orderby=Name这样的QueryString传递查询条件.排序等.你可以在任何Web API Controller中 ...

  6. ubuntu 安装 avahi服务

    sudo apt-get install avahi-daemon sudo apt-get install avahi-utils

  7. 蓝桥杯 第三届C/C++预赛真题(6) 大数乘法(数学题)

    对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的办法是:仅仅使用现 ...

  8. opencv2.4.9+VS2010配置

    opencv2.4.9 https://pan.baidu.com/s/15b5bEY65R4CptayEYVAG4A 安装包路径:D:\文件及下载相关\文档\Tencent Files\845235 ...

  9. ADO访问Oracle数据库,连接异常(Unknown error 0x800a0e7a)

    ADO访问Oracle数据库,连接异常(Unknown error 0x800a0e7a) 代码如下:执行Open过程中出现异常,信息为Unknown error 0x800a0e7a  C++ Co ...

  10. poj 1419(图的着色问题,搜索)

    题目链接:http://poj.org/problem?id=1419 思路:只怪数据太弱!直接爆搜,按顺序搜索即可. #include<iostream> #include<cst ...