此文章是对上节文章模块的补充

一,xml模块

xml是实现不同语言或程序之间进行数据交换的协议,可扩展标记语言标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。xml的格式如下,就是通过<>节点来区别数据结构的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>

python模块解析xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
导入模块 别名ET
import xml.etree.ElementTree as ET
  tree = ET.parse("test.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)

使用模块创建xml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import xml.etree.ElementTree as ET
  
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 = '33'
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'
  
et = ET.ElementTree(new_xml) #生成文档对象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
  
ET.dump(new_xml) #打印生成的格式

修改或者删除

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import xml.etree.ElementTree as ET
  
tree = ET.parse("xmltest.xml")
root = tree.getroot()
  
#修改
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')

二、shutil

高级的 文件、文件夹、压缩包 处理模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import shutil
#将文件内容拷贝到另一个文件中,可以部分内容
with open('log.log','r') as file1,open('test_con','w') as file2:
    shutil.copyfileobj('file1','file2')
#拷贝文件
shutil.copyfile('log.log','log')
#拷贝文件权限
shutil.copymode('log.log','log')
shutil.copystat(src, dst)
#拷贝状态的信息,包括:mode bits, atime, mtime, flags
 
shutil.copy(src, dst)
#拷贝文件和权限
shutil.copy2(src, dst)
#拷贝文件和状态信息
1
2
3
4
5
6
7
8
9
#将 /opt/test 下的文件打包放置当前程序目录
  
import shutil
ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/opt/test')
  
  
#将 /opt/test 下的文件打包放置 /root/目录
import shutil
ret = shutil.make_archive("/root", 'gztar', root_dir='/opt/test')

shutil 对压缩包的处理是调用 ZipFile 和 TarFile 两个模块来进行的,详细:

1
2
3
4
5
6
7
8
9
10
11
12
import zipfile
 
# 压缩
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()
 
# 解压
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall()
z.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
import tarfile
 
# 压缩
tar = tarfile.open('your.tar','w')
tar.add('/Users/wupeiqi/PycharmProjects/bbs2.zip', arcname='bbs2.zip')
tar.add('/Users/wupeiqi/PycharmProjects/cmdb.zip', arcname='cmdb.zip')
tar.close()
 
# 解压
tar = tarfile.open('your.tar','r')
tar.extractall()  # 可设置解压地址
tar.close()
复制代码

最新文章

  1. 【小白的CFD之旅】08 CFD速成之道
  2. 高性能javascript学习笔记系列(4) -算法和流程控制
  3. lua加载动态库缺乏相应的系统库
  4. Phpstorm常用设置
  5. kinect学习笔记(一)&mdash;&mdash; Openni平台的搭建~、
  6. react编译器jsxTransformer,babel
  7. Codeforces Round #197 (Div. 2)
  8. Eclipse 上安装 Maven3插件
  9. DevExpress控件学习总结 z
  10. 初次踏上GUI编程之路(有点意思,详细介绍了菜鸟的学习之路)
  11. SGU 130.Circle
  12. struts.xml中的intercepter
  13. Nexus Maven 私服搭建
  14. 计算机网络之应用层_part -1
  15. ES6 二进制数组
  16. MySQL数据库基础(二)(约束以及修改数据表)
  17. Android-FloatingActionButton
  18. svn错误:更新源码出现校验和不匹配问题
  19. &lt;&lt;linux device driver,third edition&gt;&gt; Chapter 4:Debugging Techniques
  20. CF932F Escape Through Leaf 斜率优化、启发式合并

热门文章

  1. COLORBOX文档
  2. 【kAri OJ 616】Asce的树
  3. 41.Android之图片放大缩小学习
  4. Visio绘制时序图
  5. leach和leach-c协议仿真
  6. PHP中GD2的运用,注意编码格式的改变,以及head()函数之前不能有任何html元素包括空格!!!
  7. ReactiveCocoa初步
  8. serialVersionUID要注意以下几点:
  9. phpcms v9无法连接数据库服务器,请检查配置
  10. spark1.6配置sparksql 的元数据存储到postgresql中