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

一,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()
复制代码

7.python模块补充的更多相关文章

  1. python模块补充

    一.模块补充 configparser 1.基本的读取配置文件 -read(filename) 直接读取ini文件内容 -sections() 得到所有的section,并以列表的形式返回 -opti ...

  2. 文成小盆友python-num7 -常用模块补充 ,python 牛逼的面相对象

    本篇内容: 常用模块的补充 python面相对象 一.常用模块补充 1.configparser模块 configparser 用于处理特定格式的文件,起内部是调用open()来实现的,他的使用场景是 ...

  3. python day 8: re模块补充,导入模块,hashlib模块,字符串格式化,模块知识拾遗,requests模块初识

    目录 python day 8 1. re模块补充 2. import模块导入 3. os模块 4. hashlib模块 5. 字符串格式:百分号法与format方法 6. 模块知识拾遗 7. req ...

  4. Python开发【第七篇】: 面向对象和模块补充

    内容概要 特殊成员 反射 configparser模块 hashlib模块 logging模块 异常处理 模块 包 1. 特殊成员 什么是特殊成员呢? __init_()就是个特殊的成员. 带双下划线 ...

  5. python之re模块补充和其他模块(collection、time、queue、datetime、random)

    目录 re模块补充说明 collections模块 queue模块 time模块 datetime模块 random模块 re模块补充说明 在正则表达式中,'()'的作用是进行分组,但是在re模块中, ...

  6. python 模块和包

    一,模块 1,什么是模块? 常见的场景: 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py 的后缀. 但其实 import 加载的模块分为四个通用类别: 1,使用pyt ...

  7. python开发_常用的python模块及安装方法

    adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctype ...

  8. python 模块和包以及他们的导入关系

    一 模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...

  9. 常用的python模块及安装方法

    adodb:我们领导推荐的数据库连接组件 bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheetahcherrypy:一个WEB frameworkctyp ...

随机推荐

  1. 解决already defined in .obj 的问题(定义/声明的区别)

    首先需要搞清楚什么是定义(definition ),什么是声明(declaration). 一.函数 函数的声明: int myfunc(int a,int b); 定义: int myfunc(in ...

  2. 【USACO1.1】Broken Necklace

    题意 一个环形项链,有rbw三种珠子,r代表red,b代表blue,w代表white,从任意一个位置断开,两端分别取珠子,同一端取的珠子要相同颜色,w可以染成想要的颜色,即既可当作r也可以当作b,求最 ...

  3. Android如何让真机显示debug log的调试信息

    真机默认是不开启debug log调试功能的,以前我一直用模拟器,模拟器默认是开启debug log调试功能的,那么如何让真机开启呢? 我用华为Ascend P6为例: 1.进入拨号界面,输入*#*# ...

  4. 学习笔记 BIT(树状数组)

    痛定思痛,打算切割数据结构,于是乎直接一发BIT 树状数组能做的题目,线段树都可以解决 反之则不能,不过树状数组优势在于编码简单和速度更快 首先了解下树状数组: 树状数组是一种操作和修改时间复杂度都是 ...

  5. Server Data Synchronization Via Linux rsync、rsync+inotify Between Load Balance Server

    目录 . 远程文件同步的应用场景 . rsync+crontab . rsync+inotify 1. 远程文件同步的应用场景 在负载均衡集群的应用场景中,往往在多台web server的前端有一个提 ...

  6. ECSHOP Inject PHPCode Into \library\myship.php Via \admin\template.php && \includes\cls_template.php Vul Tag_PHP_Code Execute Getshell

    目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 PHP语言作为开源社区的一员,提供了各种模板引擎,如FastTemplate,Sm ...

  7. 最新版本的DBCP数据源配置

    弄了我一下午,把该加的包都加进去了还是没用,后来把DBCP的包打开来看看才发现路径不对.配置如下: <!-- 使用dbcp配置数据源 --> <bean id="dataS ...

  8. android 自定义控件 使用declare-styleable进行配置属性(源码角度)

          android自定义styleableattrs源码 最近在模仿今日头条,发现它的很多属性都是通过自定义控件并设定相关的配置属性进行配置,于是便查询了解了下declare-styleabl ...

  9. WWDC2014总结---For产品经理们

    一年一度的苹果开发者大会WWDC2014,在北京时间6月3日凌晨1点开始了,苹果发布了iOS8.OSX10.10等,苹果比以前更加开放了,网上东西很多很杂,但缺少从产品开发角度来梳理的文章. 我根据这 ...

  10. 快速上手如何使用FluentData

    http://blog.itpub.net/29511780/viewspace-1194048/ 目录:  一.什么是ORM? 二.使用ORM的优势 三.使用ORM的缺点 四.NET下的ORM框架有 ...