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
python16_day06【类、RE模块、subprocess模块、xml模块、shelve模块】的更多相关文章
- 函数和常用模块【day06】:shelve模块(五)
本节内容 1.简述 2.shelve概念 3.shelve模块使用 4.总结 一.简述 之前我们说不管是json也好,还是pickle也好,在python3中只能dump一次和load一次,不能dum ...
- 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 ...
- Python全栈之路----常用模块----序列化(json&pickle&shelve)模块详解
把内存数据转成字符,叫序列化:把字符转成内存数据类型,叫反序列化. Json模块 Json模块提供了四个功能:序列化:dumps.dump:反序列化:loads.load. import json d ...
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
- Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)
目录: shutil logging模块 shelve configparser subprocess xml处理 yaml处理 自定义模块 一,系统标准模块: 1.shutil:是一种高层次的文件操 ...
- 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 ...
- 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时间 ...
- Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函数 os模块 hashlib模块 platform模块 csv模块
Python第十一天 异常处理 glob模块和shlex模块 打开外部程序和subprocess模块 subprocess类 Pipe管道 operator模块 sorted函 ...
- python_day7【模块configparser、XML、requests、shutil、系统命令-面向对象】之篇
python内置模块补充 一.configparser configparser:用户处理特定格式的文件,其本质是利用open打开文件 # 节点 [section1] #键值对k1 = v1 k2:v ...
随机推荐
- python 脚本撞库国内“某榴”账号
其实日常生活中我们的用户名和密码就那么几个,所以这给撞库带来了可能,本文主要给出python脚本撞库的一点粗浅代码.这里只讨论技术本生,代码中某榴的地址也已经改掉,避免被管理员误解禁言等发生,谢谢大家 ...
- ECMall中Widgets模式的布局引擎
自己做过框架的人,可能都会思考一个问题,模板引擎需要什么特性? Widgets模式,很多系统中都有出现,但对于纯开发人员,不管前端或后台人员来说,都觉得稍微麻烦了一点.因为他将界面硬生生的拆分出了很多 ...
- 上传图片时,使用JS获得图片文件大小
这个方法用于获得图片文件的大小: 在FF,Chrome,IE6,IE7,IE8可用,不支持IE9+.(如果是IE9就需要flash插件了) var getSize = function(oFile,c ...
- Github使用之git回退到某个历史版本
1. 查找历史版本 使用git log命令查看所有的历史版本,获取你git的某个历史版本的id 假设查到历史版本的id是fae6966548e3ae76cfa7f38a461c438cf75ba965 ...
- jquery datagrid设置pageSize不起作用
http://www.2cto.com/kf/201212/178098.html —————————————————————————————————————————————————————————— ...
- 【PM面试题】设计一个股价推送工具
这一轮面试时间比较短,问题在短时间内不能很全面展开,因此抓住一些关键点变得尤其重要,这里我记录下当时是怎么想这个问题的. 问题解析 子问题1:推送什么?从问题中看出我们需要推送的是股价,用户可以自定义 ...
- 在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计、SQL语句、java等层面的解决方案。
在一个千万级的数据库查寻中,如何提高查询效率?分别说出在数据库设计.SQL语句.java等层面的解决方案. 解答: 1)数据库设计方面: a. 对查询进行优化,应尽量避免全表扫描,首先应考虑在 whe ...
- 组件(Conponent)是图形用户界面最基本的部分
组件(Conponent)是图形用户界面最基本的部分,也称为构件 ,是可以以图形化的方式显示在屏幕上,并能与用户进行交互的对象,例如一个按钮,一个标签等. 组件不能独立地显示出来,必须将其放在一定的容 ...
- QT国际化,中英文等多语言界面显示的方法
在网上学习了一下QT的国际化使用方法,最后将自己试成功的方法总结例如以下: 当中遇到的问题有:生成的ts文件里 代码中的中文 有的不显示,有的显示乱码. 步骤1: 生成.ts文件,在pro项目文件 ...
- ADO访问Oracle数据库,连接异常(Unknown error 0x800a0e7a)
ADO访问Oracle数据库,连接异常(Unknown error 0x800a0e7a) 代码如下:执行Open过程中出现异常,信息为Unknown error 0x800a0e7a C++ Co ...