主页
  • 主页
  • 分类
  • 热文
  • 教程
  • 面试
  • 标签
Python

Python 基础

Python 主页
Python 概述
Python 历史
Python 功能
Python 与 C++
Python Hello World
Python 应用领域
Python 解释器及其模式
Python 环境设置
Python 虚拟环境
Python 语法
Python 变量
Python 数据类型
Python 类型转换
Python Unicode 系统
Python 文字
Python 运算符
Python 算术运算符
Python 比较运算符
Python 赋值运算符
Python 逻辑运算符
Python 位运算符
Python 成员运算符
Python 身份运算符
Python 运算符优先级
Python 注释
Python 用户输入
Python 数字
Python 布尔值

Python 控制语句

Python 控制流
Python 决策
Python if 语句
Python if-else 语句
Python 嵌套 if 语句
Python match-case 语句
Python 循环
Python For 循环
Python for-else 循环
Python while 循环
Python break 语句
Python Continue 语句
Python pass 语句
Python 嵌套循环

Python 函数和模块

Python 函数
Python 默认参数
Python 关键字参数
Python 关键字专用参数
Python 位置参数
Python 仅限位置参数
Python 任意或可变长度参数
Python 变量作用域
Python 函数注释
Python 模块
Python 内置函数

Python 字符串

Python 字符串
Python 字符串切片
Python 字符串修改
Python 字符串连接
Python 字符串格式化
Python 转义字符
Python 字符串方法
Python 字符串练习

Python 列表

Python 列表
Python 访问列表项
Python 更改列表项
Python 添加列表项
Python 删除列表项
Python 循环列表
Python 列表推导式
Python 排序列表
Python 复制列表
Python 合并列表
Python 列表方法
Python 列表练习

Python 元组

Python 元组
Python 访问元组项
Python 更新元组
Python 解包元组项
Python 循环元组
Python 合并元组
Python 元组方法
Python 元组练习

Python 集合

Python 集合
Python 访问集合项
Python 添加集合项
Python 删除集合项
Python 循环集合
Python 合并集合
Python 复制集合
Python 集合运算符
Python 集合方法
Python 集合练习

Python 字典

Python 字典
Python 访问字典项
Python 更改字典项
Python 添加字典项
Python 移除字典项
Python 字典视图对象
Python 循环字典
Python 复制字典
Python 嵌套字典
Python 字典方法
Python 字典练习

Python 数组

Python 数组
Python 访问数组项
Python 添加数组项
Python 移除数组项
Python 循环数组
Python 复制数组
Python 反转数组
Python 排序数组
Python 合并数组
Python 数组方法
Python 数组练习

Python 文件处理

Python 文件处理
Python 文件写入
Python 文件读取
Python 重命名和删除文件
Python 目录
Python 文件方法
Python 文件/目录方法
Python OS.Path 方法

Python 面向对象编程

Python OOP 概念
Python 类和对象
Python 类属性
Python 类方法
Python 静态方法
Python 构造函数
Python 访问修饰符
Python 继承
Python 多态
Python 方法重写
Python 方法重载
Python 动态绑定
Python 动态类型
Python 抽象
Python 封装
Python 接口
Python 包
Python 内部类
Python 匿名类和对象
Python 单例类
Python 包装器类
Python 枚举
Python 反射

Python 错误和异常

Python 语法错误
Python 异常处理
Python Try-Except
Python Try-Finally
Python 抛出异常
Python 异常链
Python 嵌套 try
Python 用户定义异常
Python 日志记录
Python 断言
Python 内置异常

Python 多线程

Python 多线程
Python 线程生命周期
Python 创建线程
Python 启动线程
Python 合并线程
Python 命名线程
Python 线程调度
Python 线程池
Python 主线程
Python 线程优先级
Python 守护线程
Python 线程同步

Python 同步

Python 线程间通信
Python 死锁
Python 中断线程

Python 网络

Python 网络编程
Python 套接字编程
Python URL 处理
Python 泛型

Python 杂项

Python Date and Time
Python math模块
Python 迭代器
Python 生成器
Python 闭包
Python 装饰器
Python 递归
Python 正则表达式
Python Pip
Python 数据库访问
Python 弱引用
Python 序列化
Python 模板技术
Python 输出格式化
Python 性能测量
Python 数据压缩
Python 通用网关接口
Python XML 处理
Python 用户界面(GUI)
Python 命令行参数
Python Docstrings
Python JSON
Python 发送电子邮件
Python 进一步扩展
Python 工具/实用程序
Python GUI

Python 高级概念

Python 抽象基类
Python 自定义异常
Python 高阶函数
Python 对象的内部机制
Python 内存管理
Python 元类
Python 元编程
Python 模拟与桩
Python 猴子补丁
Python 信号处理
Python 类型提示
Python 进行自动化
Python Humanize包
Python 上下文管理器
Python 协程
Python 描述符
Python 内存泄漏
Python 不可变数据结构

基础

Python 主页
Python 概述
Python 历史
Python 功能
Python 与 C++
Python Hello World
Python 应用领域
Python 解释器及其模式
Python 环境设置
Python 虚拟环境
Python 语法
Python 变量
Python 数据类型
Python 类型转换
Python Unicode 系统
Python 文字
Python 运算符
Python 算术运算符
Python 比较运算符
Python 赋值运算符
Python 逻辑运算符
Python 位运算符
Python 成员运算符
Python 身份运算符
Python 运算符优先级
Python 注释
Python 用户输入
Python 数字
Python 布尔值

控制语句

Python 控制流
Python 决策
Python if 语句
Python if-else 语句
Python 嵌套 if 语句
Python match-case 语句
Python 循环
Python For 循环
Python for-else 循环
Python while 循环
Python break 语句
Python Continue 语句
Python pass 语句
Python 嵌套循环

函数和模块

Python 函数
Python 默认参数
Python 关键字参数
Python 关键字专用参数
Python 位置参数
Python 仅限位置参数
Python 任意或可变长度参数
Python 变量作用域
Python 函数注释
Python 模块
Python 内置函数

字符串

Python 字符串
Python 字符串切片
Python 字符串修改
Python 字符串连接
Python 字符串格式化
Python 转义字符
Python 字符串方法
Python 字符串练习

列表

Python 列表
Python 访问列表项
Python 更改列表项
Python 添加列表项
Python 删除列表项
Python 循环列表
Python 列表推导式
Python 排序列表
Python 复制列表
Python 合并列表
Python 列表方法
Python 列表练习

元组

Python 元组
Python 访问元组项
Python 更新元组
Python 解包元组项
Python 循环元组
Python 合并元组
Python 元组方法
Python 元组练习

集合

Python 集合
Python 访问集合项
Python 添加集合项
Python 删除集合项
Python 循环集合
Python 合并集合
Python 复制集合
Python 集合运算符
Python 集合方法
Python 集合练习

字典

Python 字典
Python 访问字典项
Python 更改字典项
Python 添加字典项
Python 移除字典项
Python 字典视图对象
Python 循环字典
Python 复制字典
Python 嵌套字典
Python 字典方法
Python 字典练习

数组

Python 数组
Python 访问数组项
Python 添加数组项
Python 移除数组项
Python 循环数组
Python 复制数组
Python 反转数组
Python 排序数组
Python 合并数组
Python 数组方法
Python 数组练习

文件处理

Python 文件处理
Python 文件写入
Python 文件读取
Python 重命名和删除文件
Python 目录
Python 文件方法
Python 文件/目录方法
Python OS.Path 方法

面向对象编程

Python OOP 概念
Python 类和对象
Python 类属性
Python 类方法
Python 静态方法
Python 构造函数
Python 访问修饰符
Python 继承
Python 多态
Python 方法重写
Python 方法重载
Python 动态绑定
Python 动态类型
Python 抽象
Python 封装
Python 接口
Python 包
Python 内部类
Python 匿名类和对象
Python 单例类
Python 包装器类
Python 枚举
Python 反射

错误和异常

Python 语法错误
Python 异常处理
Python Try-Except
Python Try-Finally
Python 抛出异常
Python 异常链
Python 嵌套 try
Python 用户定义异常
Python 日志记录
Python 断言
Python 内置异常

多线程

Python 多线程
Python 线程生命周期
Python 创建线程
Python 启动线程
Python 合并线程
Python 命名线程
Python 线程调度
Python 线程池
Python 主线程
Python 线程优先级
Python 守护线程
Python 线程同步

同步

Python 线程间通信
Python 死锁
Python 中断线程

网络

Python 网络编程
Python 套接字编程
Python URL 处理
Python 泛型

杂项

Python Date and Time
Python math模块
Python 迭代器
Python 生成器
Python 闭包
Python 装饰器
Python 递归
Python 正则表达式
Python Pip
Python 数据库访问
Python 弱引用
Python 序列化
Python 模板技术
Python 输出格式化
Python 性能测量
Python 数据压缩
Python 通用网关接口
Python XML 处理
Python 用户界面(GUI)
Python 命令行参数
Python Docstrings
Python JSON
Python 发送电子邮件
Python 进一步扩展
Python 工具/实用程序
Python GUI

高级概念

Python 抽象基类
Python 自定义异常
Python 高阶函数
Python 对象的内部机制
Python 内存管理
Python 元类
Python 元编程
Python 模拟与桩
Python 猴子补丁
Python 信号处理
Python 类型提示
Python 进行自动化
Python Humanize包
Python 上下文管理器
Python 协程
Python 描述符
Python 内存泄漏
Python 不可变数据结构

Python XML 处理


上一章 下一章

什么是 XML?

XML(可扩展标记语言)是一种类似于 HTML 或 SGML 的标记语言。它是由万维网联盟(W3C)推荐的标准,并且作为一个开放标准提供。

XML 非常适用于在不需要基于 SQL 的后台支持的情况下跟踪小到中等数量的数据。

XML 解析器架构和 API

Python 标准库提供了一套用于处理 XML 的最小但有用的接口。所有用于 XML 处理的子模块都可以在 xml 包中找到。

  • xml.etree.ElementTree — ElementTree API,一个简单且轻量级的 XML 处理器。
  • xml.dom — DOM API 定义。
  • xml.dom.minidom — 一个最小的 DOM 实现。
  • xml.dom.pulldom — 支持构建部分 DOM 树。
  • xml.sax — SAX2 基类和便利函数。
  • xml.parsers.expat — Expat 解析器绑定。

XML 数据处理中最基本且广泛使用的 API 是 SAX 和 DOM 接口。

简单 API for XML (SAX)

在此,您注册感兴趣的事件回调,然后让解析器继续处理文档。当您的文档很大或内存有限时,此方法很有用,因为它在从磁盘读取文件的同时解析文件,并且整个文件不会存储在内存中。

Document Object Model (DOM)

这是一个由万维网联盟推荐的模型,在此模型中,整个文件被读入内存并以层次结构(基于树)的形式存储,以表示 XML 文档的所有特性。

显然,SAX 在处理大文件时无法像 DOM 那样快速地处理信息。另一方面,仅使用 DOM 可能真的会耗尽您的资源,尤其是在处理许多小文件时。

SAX 是只读的,而 DOM 允许更改 XML 文件。由于这两种不同的 API 实质上是互补的,因此在大型项目中同时使用它们没有理由。

对于所有的 XML 代码示例,我们将使用一个简单的 XML 文件 movies.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>

使用 SAX API 解析 XML

SAX 是一种基于事件驱动的 XML 解析标准接口。使用 SAX 解析 XML 通常要求您通过继承 xml.sax.ContentHandler 来创建自己的 ContentHandler。

您的 ContentHandler 处理特定的标签和属性。ContentHandler 对象提供了处理各种解析事件的方法。在其拥有者解析器解析 XML 文件时调用 ContentHandler 的方法。

startDocument 和 endDocument 方法在 XML 文件的开头和结尾被调用。characters(text) 方法通过参数 text 传递 XML 文件的字符数据。

ContentHandler 在每个元素的开始和结束时被调用。如果解析器不在命名空间模式下,则调用 startElement(tag, attributes) 和 endElement(tag) 方法;否则,调用相应的 startElementNS 和 endElementNS 方法。这里的 tag 是元素标签,attributes 是一个 Attributes 对象。

重要的方法理解

The make_parser 方法

以下方法创建一个新的解析器对象并返回它。创建的解析器对象将是系统找到的第一种解析器类型。

xml.sax.make_parser([parser_list])

参数细节:

  • parser_list — 可选参数,包含要使用的解析器列表,这些都必须实现 make_parser 方法。

The parse 方法

以下方法创建一个 SAX 解析器并使用它来解析一个文档。

xml.sax.parse(xmlfile, contenthandler[, errorhandler])

参数详情:

  • xmlfile — 这是要从中读取的 XML 文件名称。
  • contenthandler — 这必须是一个 ContentHandler 对象。
  • errorhandler — 如果指定了错误处理器,那么 errorhandler 必须是一个 SAX ErrorHandler 对象。

The parseString 方法

还有一个方法创建一个 SAX 解析器并解析指定的 XML 字符串。

xml.sax.parseString(xmlstring, contenthandler[, errorhandler])

参数详情:

  • xmlstring — 这是要从中读取的 XML 字符串。
  • contenthandler — 这必须是一个 ContentHandler 对象。
  • errorhandler — 如果指定了错误处理器,那么 errorhandler 必须是一个 SAX ErrorHandler 对象。

示例

import xml.sax

class MovieHandler(xml.sax.ContentHandler):
   def __init__(self):
      self.CurrentData = ""
      self.type = ""
      self.format = ""
      self.year = ""
      self.rating = ""
      self.stars = ""
      self.description = ""

   # 当元素开始时调用
   def startElement(self, tag, attributes):
      self.CurrentData = tag
      if tag == "movie":
         print("*****Movie*****")
         title = attributes["title"]
         print("Title:", title)

   # 当元素结束时调用
   def endElement(self, tag):
      if self.CurrentData == "type":
         print("Type:", self.type)
      elif self.CurrentData == "format":
         print("Format:", self.format)
      elif self.CurrentData == "year":
         print("Year:", self.year)
      elif self.CurrentData == "rating":
         print("Rating:", self.rating)
      elif self.CurrentData == "stars":
         print("Stars:", self.stars)
      elif self.CurrentData == "description":
         print("Description:", self.description)
      self.CurrentData = ""

   # 当读取到字符时调用
   def characters(self, content):
      if self.CurrentData == "type":
         self.type = content
      elif self.CurrentData == "format":
         self.format = content
      elif self.CurrentData == "year":
         self.year = content
      elif self.CurrentData == "rating":
         self.rating = content
      elif self.CurrentData == "stars":
         self.stars = content
      elif self.CurrentData == "description":
         self.description = content

if __name__ == "__main__":
   # 创建一个 XMLReader
   parser = xml.sax.make_parser()
   
   # 关闭命名空间
   parser.setFeature(xml.sax.handler.feature_namespaces, 0)
   
   # 覆盖默认的 ContextHandler
   Handler = MovieHandler()
   parser.setContentHandler(Handler)
   
   parser.parse("movies.xml")

此示例会产生以下输出:

*****Movie*****
Title: Enemy Behind
Type: War, Thriller
Format: DVD
Year: 2003
Rating: PG
Stars: 10
Description: Talk about a US-Japan war
*****Movie*****
Title: Transformers
Type: Anime, Science Fiction
Format: DVD
Year: 1989
Rating: R
Stars: 8
Description: A schientific fiction
*****Movie*****
Title: Trigun
Type: Anime, Action
Format: DVD
Rating: PG
Stars: 10
Description: Vash the Stampede!
*****Movie*****
Title: Ishtar
Type: Comedy
Format: VHS
Rating: PG
Stars: 2
Description: Viewable boredom

有关 SAX API 文档的完整详细信息,请参考标准 Python SAX API 文档。

使用 DOM API 解析 XML

文档对象模型(DOM)是万维网联盟(W3C)制定的一种跨语言 API,用于访问和修改 XML 文档。

DOM 对于随机访问应用程序来说极其有用。SAX 只允许您一次查看文档的一部分。如果您正在查看一个 SAX 元素,您就无法访问另一个元素。

以下是使用 xml.dom 模块快速加载 XML 文档并创建 minidom 对象的最简单方式。minidom 对象提供了一个简单的解析器方法,可以快速地从 XML 文件创建 DOM 树。

示例代码调用了 minidom 对象的 parse(file [,parser]) 函数来解析 XML 文件,该文件由 file 指定,并将其转换成 DOM 树对象。

from xml.dom.minidom import parse
import xml.dom.minidom

# 使用 minidom 解析器打开 XML 文档
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
   print ("根元素 : %s" % collection.getAttribute("shelf"))

# 获取集合中的所有电影
movies = collection.getElementsByTagName("movie")

# 打印每部电影的详情。
for movie in movies:
   print ("*****Movie*****")
   if movie.hasAttribute("title"):
      print ("标题: %s" % movie.getAttribute("title"))

   type = movie.getElementsByTagName('type')[0]
   print ("类型: %s" % type.childNodes[0].data)
   format = movie.getElementsByTagName('format')[0]
   print ("格式: %s" % format.childNodes[0].data)
   rating = movie.getElementsByTagName('rating')[0]
   print ("评级: %s" % rating.childNodes[0].data)
   description = movie.getElementsByTagName('description')[0]
   print ("描述: %s" % description.childNodes[0].data)

此代码会产生以下输出:

根元素 : New Arrivals
*****Movie*****
标题: Enemy Behind
类型: War, Thriller
格式: DVD
评级: PG
描述: Talk about a US-Japan war
*****Movie*****
标题: Transformers
类型: Anime, Science Fiction
格式: DVD
评级: R
描述: A schientific fiction
*****Movie*****
标题: Trigun
类型: Anime, Action
格式: DVD
评级: PG
描述: Vash the Stampede!
*****Movie*****
标题: Ishtar
类型: Comedy
格式: VHS
评级: PG
描述: Viewable boredom

有关 DOM API 文档的完整详细信息,请参考标准 Python DOM API 文档。

ElementTree XML API

xml 包中有一个 ElementTree 模块。这是一个简单且轻量级的 XML 处理器 API。

XML 是一种树状分层数据格式。此模块中的 'ElementTree' 将整个 XML 文档视为一棵树。'Element' 类代表了树中的单个节点。对 XML 文件的读写操作是在 ElementTree 级别进行的。与单个 XML 元素及其子元素的交互则是在 Element 级别完成的。

创建 XML 文件

树是从根开始的层次结构,后面跟着其他元素。每个元素都是使用此模块的 Element() 函数创建的。

import xml.etree.ElementTree as et
e = et.Element('name')

每个元素都由标签和属性 attrib 特性定义,其中 attrib 是一个字典对象。对于树的起始元素,attrib 是一个空字典。

>>> root = et.Element('employees')
>>> root.tag
'employees'
>>> root.attrib
{}

现在您可以设置一个或多个子元素添加到根元素下。每个子元素可以有一个或多个子元素。使用 SubElement() 函数并定义其 text 属性来添加它们。

child = et.Element("employee")
nm = et.SubElement(child, "name")
nm.text = student.get('name')
age = et.SubElement(child, "salary")
age.text = str(student.get('salary'))

每个子元素通过 append() 函数添加到根元素:

root.append(child)

在添加所需数量的子元素后,使用 ElementTree() 函数构造树对象:

tree = et.ElementTree(root)

整个树结构通过树对象的 write() 函数写入二进制文件:

f = open('employees.xml', "wb")
tree.write(f)

示例

在此示例中,从字典项列表构造了一个树。每个字典项包含了描述学生数据结构的关键值对。如此构造的树被写入 'myfile.xml' 中。

import xml.etree.ElementTree as et
employees = [{'name': 'aaa', 'age': 21, 'sal': 5000}, {'name': 'xyz', 'age': 22, 'sal': 6000}]
root = et.Element("employees")
for employee in employees:
   child = et.Element("employee")
   root.append(child)
   nm = et.SubElement(child, "name")
   nm.text = employee.get('name')
   age = et.SubElement(child, "age")
   age.text = str(employee.get('age'))
   sal = et.SubElement(child, "sal")
   sal.text = str(employee.get('sal'))
tree = et.ElementTree(root)
with open('employees.xml', "wb") as fh:
   tree.write(fh)

'myfile.xml' 存储在当前工作目录中。

<employees><employee><name>aaa</name><age>21</age><sal>5000</sal></employee><employee><name>xyz</name><age>22</age><sal>6000</sal></employee></employee>

解析 XML 文件

我们现在重新读取上面示例中创建的 'myfile.xml'。为此目的,ElementTree 模块中的下列函数将被使用:

  • ElementTree() — 此函数重载以将元素的层次结构读取到树对象。
  • tree = et.ElementTree(file='students.xml')
  • getroot() — 此函数返回树的根元素。
  • root = tree.getroot()

您可以获取一个元素下面的一级子元素列表。

children = list(root)

在下面的示例中,'myfile.xml' 的元素和子元素被解析成字典项列表。

import xml.etree.ElementTree as et
tree = et.ElementTree(file='employees.xml')
root = tree.getroot()
employees = []
children = list(root)
for child in children:
   employee = {}
   pairs = list(child)
   for pair in pairs:
      employee[pair.tag] = pair.text
   employees.append(employee)
print(employees)

它将产生以下输出:

[{'name': 'aaa', 'age': '21', 'sal': '5000'}, {'name': 'xyz', 'age': '22', 'sal': '6000'}]

修改 XML 文件

我们将使用 Element 的 iter() 函数。它为给定的标签创建一个以当前元素为根的树迭代器。迭代器按文档顺序(深度优先)迭代此元素及其所有子元素。

让我们为所有 'sal' 子元素构建迭代器,并将每个 'sal' 标签的文本增加 100。

import xml.etree.ElementTree as et
tree = et.ElementTree(file='students.xml')
root = tree.getroot()
for x in root.iter('sal'):
   s = int(x.text)
   s += 100
   x.text = str(s)
with open("employees.xml", "wb") as fh:
   tree.write(fh)

我们的 'employees.xml' 将相应地被修改。我们也可以使用 set() 来更新某个键的值。

x.set('marks', str(mark))
上一章 下一章
阅读号二维码

关注阅读号

联系二维码

联系我们

© 2024 Yoagoa. All rights reserved.

粤ICP备18007391号

站点地图