Python基础之读写xml总结
参考文章:https://blog.csdn.net/weixin_42749767/article/details/82770563
先介绍xml.dom.minidom包,有一个读写的例子
read_write_xml.py
from xml.dom.minidom import parse
import xml.dom.minidom
import os def is_xml_exist(xml_path):
xml_exist = os.path.exists(xml_path)
if not xml_exist:
return False
return True """
movie.xml
<collection shelf="New Arrivals">
<movie title="Enemy Behind">
<type>War, Thriller</type>
<format>DVD</format>
<year>2003</year>
<rating>PG</rating>
<stars>10</stars>
<description>Talk about a US-Japan war</description>
</movie>
<movie title="Transformers">
<type>Anime, Science Fiction</type>
<format>DVD</format>
<year>1989</year>
<rating>R</rating>
<stars>8</stars>
<description>A schientific fiction</description>
</movie>
<movie title="Trigun">
<type>Anime, Action</type>
<format>DVD</format>
<episodes>4</episodes>
<rating>PG</rating>
<stars>10</stars>
<description>Vash the Stampede!</description>
</movie>
<movie title="Ishtar">
<type>Comedy</type>
<format>VHS</format>
<rating>PG</rating>
<stars>2</stars>
<description>Viewable boredom</description>
</movie>
</collection>
""" def read_movie_xml():
path = "movie.xml"
if not is_xml_exist(path):
print("%s is not exist" % path)
else:
# 使用minidom解析器打开XML文档
open_xml = parse(path)
root_node = open_xml.documentElement shelf_attrib = "shelf"
if root_node.hasAttribute(shelf_attrib):
print("Lable: %s\tAttrib: %s\t\tValue: %s" % (
root_node.nodeName, shelf_attrib, root_node.getAttribute(shelf_attrib)))
print("")
# 在集合中获取所有电影
movie_node = "movie"
movies = root_node.getElementsByTagName(movie_node) # 打印每部电影的详细信息
for movie in movies:
print("**** Movie ****")
if movie.hasAttribute("title"):
print("Title: %s" % movie.getAttribute("title")) type_movie = movie.getElementsByTagName('type')[0]
print("Type: %s" % type_movie.childNodes[0].data) format_movie = movie.getElementsByTagName('format')[0]
print("Format: %s" % format_movie.childNodes[0].data) rating_movie = movie.getElementsByTagName('rating')[0]
print("Rating: %s" % rating_movie.childNodes[0].data) descrip_movie = movie.getElementsByTagName('description')[0]
print("Rating: %s" % descrip_movie.childNodes[0].data) print("") if __name__ == "__main__":
read_movie_xml()
运行结果:
用到的知识点:
1. 导入xml包:
from xml.dom.minidom import parse
2. 打开xml文件:
open_xml = parse(path)
root_node = open_xml.documentElement
3. 获取节点名称:
root_node.nodeName
4. 判断节点属性是否存在:
root_node.hasAttribute(shelf_attrib)
5. 获取节点属性:
root_node.getAttribute(shelf_attrib)
6. 获取子节点对象:
root_node.getElementsByTagName(movie_node)
7. 获取文本节点的文本信息:
type_movie.childNodes[0].data
以上语句务必正确使用,运行第二步,xml必须已经存在,运行第六步,子节点的标签必须存在,运行第七步,此节点必须是文本节点,否则都会出现异常。
Python基础之读写xml总结的更多相关文章
- python 基础-文件读写'r' 和 'rb'区别
原文链接: python基础-文件读写'r' 和 'rb'区别 一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r ...
- python利用lxml读写xml格式文件
之前在转换数据集格式的时候需要将json转换到xml文件,用lxml包进行操作非常方便. 1. 写xml文件 a) 用etree和objectify from lxml import etree, o ...
- python基础-文件读写'r' 和 'rb'区别
一.Python文件读写的几种模式: r,rb,w,wb 那么在读写文件时,有无b标识的的主要区别在哪里呢? 1.文件使用方式标识 'r':默认值,表示从文件读取数据.'w':表示要向文件写入数据,并 ...
- python基础之读取xml
python怎么操作xml文件详细介绍链接:https://www.jb51.net/article/50812.htm 从结构上来说,xml很像常见的HTML超文本标记语言.不过超文本语言被设计用来 ...
- python基础 - 文件读写
完成功能: 从指定位置读文件到控制台 #! /usr/bin/python # coding=utf- 方法一. try: f = open ('/root/python/file/001.txt', ...
- python 基础 ---- 文件读写
文件是一种存储在存储存储媒介上的信息或数据 常用的文件类型 文件 的打开关闭 close() 关闭文件 文件的打开路径 绝对路径 : 文件在操作系统中标准的存放路径 相对路径: 与目前引用文件的相对位 ...
- python基础之文件读写
python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使用os模块的一些方法如下: 得到 ...
- 第二篇:python基础之文件读写
python基础之文件读写 python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使 ...
- 七. Python基础(7)--文件的读写
七. Python基础(7)--文件的读写 1 ● 文件读取的知识补充 f = open('file', encoding = 'utf-8') content1 = f.read() content ...
随机推荐
- 【NX二次开发】常用的标准对话框
1.uc1601 单按钮模态对话框 1 //来自"王牌飞行员_里海"的测试源码(qq群753801561) 2 extern DllExport void ufusr(char * ...
- 08:'my_tag' is not a registered tag library. Must be one of
确保每次修改模板标签时都重新启动 Django 开发服务器(或确保它自己重新启动).如果服务器没有重新启动,Django 将不会注册标签. 从 django 1.9 开始,您可以在如下设置中加载这些新 ...
- 工作中,如何衡量一个人的 JavaScript 编码水平?
1.立即执行函数 立即执行函数,即Immediately Invoked Function Expression (IIFE),正如它的名字,就是创建函数的同时立即执行.它没有绑定任何事件,也无需等待 ...
- JDK并发包一
JDK并发包一 同步控制是并发程序必不可少的重要手段,synchronized是一种最简单的控制方法.但JDK提供了更加强大的方法----重入锁 重入锁 重入锁使用java.util.concurre ...
- redhat6版本网卡绑定做bond
1.编写bond0配置文件 cd /etc/sysconfig/network-scripts(进入网卡配置文件路径) vi ifc-bond0(编辑bond0的配置文件,具体如下) DEVICE=b ...
- This application failed to start because no Qt platform plugin could be initialized
今天在直接运行QT生成的.exe遇到了一个错误:This application failed to start because no Qt platform plugin could be init ...
- CentOS7-磁盘扩容(LVM-非空目录拓展卷空间大小)
查看存储情况 $ df -kh 查看磁盘情况 $ fdisk -l 创建分区(注:可操作存储上限2TB) $ fdisk /dev/sdb 根据提示,依次输入"n","p ...
- phpstorm之"Can not run PHP Code Sniffer"
前言 其实我是不太愿意写这种工具使用博客的,因为实在没有营养,只是有些简单问题,搜索一番,却始终找不到答案,遂以博客记录下来,希望后面的人,可以省去搜索之苦. 相信你搜到这篇博客,肯定是已经安装好了P ...
- 0shell变量
1.定义变量 2.使用变量 3.修改变量的值 4.将命令的结果赋值给变量 5.只读变量 6.删除变量 一.变量 1.定义变量 在 Bash shell 中,每一个变量的值都是字符串,无论你给变量赋值时 ...
- ReadyAPI 测试工具和创建管理
通过测试加速API质量APIs 和微服务正在改变组织在数字世界中开展业务的方式,对它们进行测试 比以往任何时候都更加重要 ReadyAPI测试工具是创建.管理.并运行自动化测试REST.SOAP.Gr ...