Python3 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
24
25
26
|
<data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year updated_by="Alex">2009</year> <gdppc>141100</gdppc> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year updated_by="Alex">2012</year> <gdppc>59900</gdppc> <neighbor direction="N" name="Malaysia" /> </country> <country name="Panama"> <rank updated="yes">69</rank> <year updated_by="Alex">2012</year> <gdppc>13600</gdppc> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> <info> <population>8</population> <size>960</size> </info> </country></data> |
xml数据处理
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
import xml.etree.ElementTree as ET'''xml 数据的处理 '''tree = ET.parse("xmltest.xml")root = tree.getroot() #数据内存地址print(root.tag) #标签'''遍历所有数据'''for i in root: print(i.tag,i.attrib) #attrib 获取属性名 for k in i: print(k.tag,k.attrib,k.text) #text 文本内容''' 遍历某一个标签的值 '''for ta in root.iter("year"): print(ta.tag,ta.attrib,ta.text) |
XML数据的创建
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
'''xml 数据的创建 '''new_xml = ET.Element("personinfolist") #Element 根节点personinfo = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "yes"})name = ET.SubElement(personinfo, "name") #SubElement 子节点name.text = "Alex Li"age = ET.SubElement(personinfo, "age", attrib={"checked": "no"})sex = ET.SubElement(personinfo, "sex")age.text = '56'personinfo2 = ET.SubElement(new_xml, "personinfo", attrib={"enrolled": "no"})name = ET.SubElement(personinfo2, "name")name.text = "Oldboy Ran"age = ET.SubElement(personinfo2, "age")age.text = '19'et = ET.ElementTree(new_xml) # 生成文档对象et.write("test.xml", encoding="utf-8", xml_declaration=True)""" xml_declaration 声明xml文件类型 """ET.dump(new_xml) # 打印生成的格式 |
XML数据的修改
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
'''xml 数据的修改 '''for node in root.iter('year'): new_year = int(node.text) + 1 node.text = str(new_year) node.set("updated_by", "Alex")tree.write("xmltest.xml")# 删除nodefor country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country)tree.write('output.xml') |
Python3 xml模块的增删改查的更多相关文章
- java对xml文件做增删改查------摘录
java对xml文件做增删改查 package com.wss; import java.io.File;import java.util.ArrayList;import java.util.Lis ...
- 基于pymysql模块的增删改查
上课笔记 重点:(熟练)多表查询创建存储过程原生sql索引原理 pymysql 封装好的客户端cursor 底层就是一个send操作commit 告诉mysql真的要完成修改操作(不然修改不会生效)e ...
- 使用dom4j对xml文件进行增删改查
1.使用dom4j技术对dom_demo.xml进行增删改查 首选要下载dom4j的jar包 在官网上找不到,网上搜索了一下在这个链接:http://sourceforge.net/projects/ ...
- XML(五)dom4j增删改查
book2.xml <? xml version="1.0" encoding="UTF-8"?> <书架> <书> < ...
- python3连接MySQL实现增删改查
PyMySQL 安装 在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装. PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL. 如果还未安 ...
- 用pickle模块实现“增删改查”的简易功能
pickle的作用: 1:pickle.dump(dict,file)把字典转为二进制存入文件. 2:pickle.load(file)把文件二进制内容转为字典 import pickle # 增 d ...
- Asp.Net 操作XML文件的增删改查 利用GridView
不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 ...
- java图书管理的一个小模块(增删改查,不使用数据库)
图书管理模块:某图书管需要对图书进行信息化管理,要求管理员能够进行新增图书,能按照书名进行模糊查看图书能进行价格统计 系统实现如下:1.新增2.查询3.统计价格 1请输入新书:图书号,书名,作者,价格 ...
- dom4j解析xml文档(增删改查)
package itcast.dom4j; import java.io.File; import java.io.FileOutputStream; import java.io.FileWrite ...
随机推荐
- IO Model- 同步,异步,阻塞,非阻塞
同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别?这个问题其实不同的人给出 ...
- 【Python】从简单案列中揭示常用内置函数以及数据类型
前面提到了BIF(内置函数)这个概念,什么是内置函数,就是python已经定义好的函数,不需要人为再自己定义,直接拿来就可以用的函数,那么都有哪些BIF呢? 可以在交互式界面(IDLE)输入这段代码, ...
- CentOS LVM逻辑卷管理
在CentOS 挂载(U盘NTFS格式,新硬盘,增加交换分区,扩展根分区等)中扩展根分区部分用的就是LVM逻辑卷管理来进行扩展的. 1.为什么会有逻辑卷管理 传统磁盘管理是直接对硬盘分区进行访问,你如 ...
- Javascript基础之-强制类型转换(二)
思考下面这个问题: console.log(+"123"); // 123 console.log(-"123"); // -123 console.log(+ ...
- POJ3264:Balanced Lineup——题解+st表解释
我早期在csdn的博客之一,正好复习st表就拿过来.http://write.blog.csdn.net/mdeditor#!postId=63713810 这道题其实本身不难(前提是你得掌握线段树或 ...
- HDOJ.1263 水果(map)
水果 点我跳转到题面 点我一起学习STL-MAP 题意分析 给出多组测试数据,每组数据有多条信息.分别是水果种类,地点,和水果数目.每组信息要按照样例输出,并且输出要按照地点->水果种类的字典序 ...
- 命令:ln 使用方法
http://linux.chinaunix.net/man/2004-10-06/45.shtml 指令名称 : ln 使用权限 : 所有使用者 使用方式 : ln [options] source ...
- [zhuan]arm中的汇编指令
http://blog.csdn.net/qqliyunpeng/article/details/45116615 一. 带点的(一般都是ARM GNU伪汇编指令) 1. ".text& ...
- Extjs 动态修改gridPanel列头信息以及store数据的方法
1 /*******************************checkbox按钮 历史报警信息**************************************/ var check ...
- Codeforces Round #337 (Div. 2)B
B. Vika and Squares time limit per test 2 seconds memory limit per test 256 megabytes input standard ...