一、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. R语言中将hello打印10次的两种方法

    我们有两种方法来做这件事情: 1.for结构 for循环重复的执行一个语句,直到某个变量的值不再包含在序列seq中为止. 语法: for (var in seq) statement 例如: > ...

  2. linux输入法

    sudo apt-get install fcitx-table-wbpy安装fcitx 中文是灰色的就拖到第一个 桌面右上角有个设置,可以添加拼音 拼音特别难用

  3. Attention Mechanism

    首先介绍Attention机制: 转自:http://blog.csdn.net/malefactor/article/details/50550211 上面讲的是Soft Attention Mod ...

  4. ubuntu14.04安装pycurl

    一. 安装依赖项 sudo apt-get install libcurl4-gnutls-dev 二. 安装pycurl pip install pycurl 三. 检验是否安装成功 进入pytho ...

  5. EJB EJB定义了一组可重用的组件:Enterprise Beans

    EJB EJB定义了一组可重用的组件:Enterprise Beans.开发人员可以利用这些组件,像搭积木一样建立分布式应用.

  6. .net mvc 下实现移动架构display mode

    最近要求做支持移动端网站 我们的设计已经把移动端UI给我了,但是我还没想好改用哪种方式支持移动端 1.常见的就是2级域名m.baidu.com,框架根据访问者设备,来选择是否跳转到移动站 2.同域名的 ...

  7. 设置grid高度

    $("#jqxSalaryCalculation").jqxGrid({ height: $("#jqxTree").height() - 73 });

  8. 【ARDUINO】HC-05蓝牙不配对问题

    除了刷主从之外,不配对的原因有1:已经配对其他设备,需用AT+RMAAD来移除.2.默认为蓝牙由绑定指令设置,需改为任意地址连接模式AT+CMODE=1 //#define AT 2 #define ...

  9. mstsc远程登录设置

    mstsc终于可以连上了, 1.系统属性 远程允许, 2.开启三个服务: Remote Desktop ConfigurationRemote Desktop ServicesRemote Deskt ...

  10. 再谈iOS 7的手势滑动返回功能

    本文转载至 http://blog.csdn.net/jasonblog/article/details/28282147  之前随手写过一篇<使用UIScreenEdgePanGestureR ...