文章目录
- ET 提供了两种主要解析方式: import xml.etree.ElementTree as ET # 方法1:解析文件(推荐用于文件) tree = ET.parse(‘books.xml’) # 返回 ElementTree 对象 root = tree.getroot() # 获取根元素 # 方法2:解析字符串(用于从网络或内存读取) xml_string = “””<?xml version=”1.0″?> <library> <book id=”001″> <title>Python编程</title> </book> </library>””” root = ET.fromstring(xml_string) # 直接返回根元素
- 假设我们有如下 books.xml 文件: <?xml version=”1.0″ encoding=”UTF-8″?> <library location=”北京”> <book id=”001″ category=”编程”> <title>Python编程:从入门到实践</title> <author>埃里克·马瑟斯</author> <price currency=”CNY”>89.00</price> <publish_date>2020-05</publish_date> </book> <book id=”002″ category=”小说”> <title>百年孤独</title> <author>加西亚·马尔克斯</author> <price currency=”CNY”>55.00</price> <publish_date>2011-06</publish_date> </book> <book id=”003″ category=”编程”> <title>流畅的Python</title> <author>卢西亚诺·拉马略</author> <price currency=”CNY”>139.00</price> <publish_date>2017-05</publish_date> </book> </library> 解析代码: import xml.etree.ElementTree as ET def parse_library(): “””解析图书馆 XML 文件””” try: # 解析 XML 文件 tree = ET.parse(‘books.xml’) root = tree.getroot() print(f”根元素标签: {root.tag}”) print(f”根元素属性: {root.attrib}”) print(f”子元素数量: {len(root)}”) print(“-” * 50) # 遍历所有 book 元素 for book in root.findall(‘book’): book_id = book.get(‘id’) # 获取属性 category = book.get(‘category’) title = book.find(‘title’).text # 获取文本内容 author = book.find(‘author’).text price_elem = book.find(‘price’) price = price_elem.text currency = price_elem.get(‘currency’) print(f”图书ID: {book_id}”) print(f”类别: {category}”) print(f”书名: {title}”) print(f”作者: {author}”) print(f”价格: {currency} {price}”) print(“-” * 50) except ET.ParseError as e: print(f”XML 解析错误: {e}”) except FileNotFoundError: print(“文件未找到,请确保 books.xml 存在”) if __name__ == “__main__”: parse_library() 输出结果: 根元素标签: library根元素属性: {'location': '北京'}子元素数量: 3————————————————–图书ID: 001类别: 编程书名: Python编程:从入门到实践作者: 埃里克·马瑟斯价格: CNY 89.00————————————————–…
- 对于大型 XML 文件,使用迭代器避免一次性加载所有数据: import xml.etree.ElementTree as ET def iterate_xml(): “””使用迭代器遍历大型 XML””” # iterparse 在解析时生成事件,适合大文件 context = ET.iterparse(‘books.xml’, events=(‘start’, ‘end’)) context = iter(context) event, root = next(context) book_count = 0 for event, elem in context: # ‘start’ 事件:元素开始 # ‘end’ 事件:元素结束(此时元素已完整) if event == ‘end’ and elem.tag == ‘book’: book_count += 1 title = elem.find(‘title’).text print(f”处理第 {book_count} 本书: {title}”) # 处理完后清除元素释放内存 elem.clear() root.clear() print(f”总共处理了 {book_count} 本书”) # 递归遍历所有元素 def recursive_walk(element, level=0): “””递归打印 XML 结构””” indent = ” ” * level print(f”{indent}<{element.tag}> {element.attrib if element.attrib else ”}”) if element.text and element.text.strip(): print(f”{indent} 文本: {element.text.strip()}”) for child in element: recursive_walk(child, level + 1) print(f”{indent}</{element.tag}>”) # 使用示例 tree = ET.parse(‘books.xml’) recursive_walk(tree.getroot())
- def traverse_by_level(root): “””按层级遍历(广度优先)””” from collections import deque queue = deque([(root, 0)]) while queue: elem, level = queue.popleft() indent = ” ” * level print(f”{indent}[{level}] {elem.tag}: {elem.attrib}”) for child in elem: queue.append((child, level + 1)) traverse_by_level(tree.getroot())
- import xml.etree.ElementTree as ET def modify_xml(): “””修改 XML 内容””” tree = ET.parse(‘books.xml’) root = tree.getroot() # 1. 修改属性 root.set(‘updated’, ‘2024-01-01’) root.set(‘location’, ‘上海’) # 修改现有属性 # 2. 修改元素文本 for book in root.findall(‘book’): price_elem = book.find(‘price’) old_price = float(price_elem.text) # 打 8 折 new_price = old_price * 0.8 price_elem.text = f”{new_price:.2f}” price_elem.set(‘discount’, ‘0.8’) # 3. 添加新元素 for book in root.findall(‘book’): stock = ET.SubElement(book, ‘stock’) stock.text = ‘100’ stock.set(‘warehouse’, ‘A1’) # 4. 删除元素 # 删除第一本书的 publish_date first_book = root.find(‘book’) publish_date = first_book.find(‘publish_date’) if publish_date is not None: first_book.remove(publish_date) # 5. 保存修改 tree.write(‘books_modified.xml’, encoding=’utf-8′, xml_declaration=True, short_empty_elements=False) print(“修改完成,已保存到 books_modified.xml”) modify_xml()
- class XMLModifier: “””XML 批量修改器””” def __init__(self, input_file: str): self.tree = ET.parse(input_file) self.root = self.tree.getroot() self.modified = False def update_prices(self, increase_rate: float = 0.1): “””批量更新价格””” for price_elem in self.root.iter(‘price’): old_price = float(price_elem.text) new_price = old_price * (1 + increase_rate) price_elem.text = f”{new_price:.2f}” price_elem.set(‘updated’, ‘true’) self.modified = True print(f”已更新所有价格,涨幅 {increase_rate*100}%”) def add_element_to_all(self, tag: str, text: str, attrib: dict = None): “””为所有 book 添加子元素””” attrib = attrib or {} for book in self.root.findall(‘book’): elem = ET.SubElement(book, tag, attrib) elem.text = text self.modified = True print(f”已为所有图书添加 <{tag}> 元素”) def remove_element_by_tag(self, tag: str): “””删除所有指定标签的元素””” count = 0 for parent in self.root.iter(): for child in list(parent): # 使用 list 避免迭代时修改 if child.tag == tag: parent.remove(child) count += 1 self.modified = True print(f”已删除 {count} 个 <{tag}> 元素”) def save(self, output_file: str = None): “””保存文件””” if not self.modified: print(“没有修改需要保存”) return output = output_file or ‘modified.xml’ self.tree.write(output, encoding=’utf-8′, xml_declaration=True) print(f”已保存到: {output}”) # 使用示例 modifier = XMLModifier(‘books.xml’) modifier.update_prices(0.15) # 涨价 15% modifier.add_element_to_all(‘status’, ‘在售’, {‘available’: ‘true’}) modifier.save(‘books_updated.xml’)
- import xml.etree.ElementTree as ET from datetime import datetime def create_library_xml(): “””创建图书馆 XML 文件””” # 创建根元素 root = ET.Element(‘library’) root.set(‘version’, ‘1.0’) root.set(‘created’, datetime.now().isoformat()) # 添加注释 comment = ET.Comment(‘ 这是一个自动生成的图书馆数据文件 ‘) root.append(comment) # 创建图书数据 books_data = [ { ‘id’: ‘004’, ‘category’: ‘科幻’, ‘title’: ‘三体’, ‘author’: ‘刘慈欣’, ‘price’: ‘98.00’, ‘currency’: ‘CNY’, ‘tags’: [‘雨果奖’, ‘科幻经典’, ‘系列作品’] }, { ‘id’: ‘005’, ‘category’: ‘技术’, ‘title’: ‘深度学习’, ‘author’: ‘伊恩·古德费洛’, ‘price’: ‘168.00’, ‘currency’: ‘CNY’, ‘tags’: [‘AI’, ‘机器学习’, ‘教材’] } ] for book_data in books_data: # 创建 book 元素 book = ET.SubElement(root, ‘book’) book.set(‘id’, book_data[‘id’]) book.set(‘category’, book_data[‘category’]) # 添加子元素 title = ET.SubElement(book, ‘title’) title.text = book_data[‘title’] author = ET.SubElement(book, ‘author’) author.text = book_data[‘author’] price = ET.SubElement(book, ‘price’) price.text = book_data[‘price’] price.set(‘currency’, book_data[‘currency’]) # 添加标签列表 tags = ET.SubElement(book, ‘tags’) for tag_text in book_data[‘tags’]: tag = ET.SubElement(tags, ‘tag’) tag.text = tag_text # 创建 ElementTree 对象 tree = ET.ElementTree(root) # 美化输出(缩进) ET.indent(tree, space=’ ‘, level=0) # 保存到文件 tree.write(‘new_library.xml’, encoding=’utf-8′, xml_declaration=True, short_empty_elements=False) print(“成功创建 new_library.xml”) # 同时返回字符串形式 return ET.tostring(root, encoding=’unicode’) xml_string = create_library_xml() print(“n生成的 XML 内容:”) print(xml_string) 生成的 XML 结构: <?xml version=’1.0′ encoding=’utf-8′?> <library created=”2024-01-15T10:30:00″ version=”1.0″> <!– 这是一个自动生成的图书馆数据文件 –> <book category=”科幻” id=”004″> <title>三体</title> <author>刘慈欣</author> <price currency=”CNY”>98.00</price> <tags> <tag>雨果奖</tag> <tag>科幻经典</tag> <tag>系列作品</tag> </tags> </book> … </library>
- def create_element_factory(): “””使用工厂模式创建 XML””” def create_book(id, title, author, **kwargs): “””创建图书元素的工厂函数””” book = ET.Element(‘book’, {‘id’: id}) ET.SubElement(book, ‘title’).text = title ET.SubElement(book, ‘author’).text = author for key, value in kwargs.items(): if isinstance(value, dict): # 带属性的元素 elem = ET.SubElement(book, key, value.get(‘attrib’, {})) elem.text = value.get(‘text’, ”) else: ET.SubElement(book, key).text = str(value) return book # 构建 XML root = ET.Element(‘catalog’) book1 = create_book( ‘006’, ‘Python数据科学手册’, ‘杰克·万托布拉斯’, price={‘text’: ‘128.00’, ‘attrib’: {‘currency’: ‘CNY’}}, publisher=’人民邮电出版社’, year=’2020′ ) root.append(book1) # 保存 tree = ET.ElementTree(root) ET.indent(tree, space=’ ‘) tree.write(‘catalog.xml’, encoding=’utf-8′, xml_declaration=True) print(“创建 catalog.xml 成功”) create_element_factory()
- import xml.etree.ElementTree as ET import os class ConfigManager: “””XML 配置文件管理器””” CONFIG_FILE = ‘app_config.xml’ def __init__(self): self.tree = None self.root = None self._load_or_create() def _load_or_create(self): “””加载或创建配置文件””” if os.path.exists(self.CONFIG_FILE): self.tree = ET.parse(self.CONFIG_FILE) self.root = self.tree.getroot() else: self.root = ET.Element(‘configuration’) self.root.set(‘version’, ‘1.0’) self.tree = ET.ElementTree(self.root) self._save() def _save(self): “””保存配置””” ET.indent(self.tree, space=’ ‘) self.tree.write(self.CONFIG_FILE, encoding=’utf-8′, xml_declaration=True) def get(self, section: str, key: str, default=None): “””获取配置值””” section_elem = self.root.find(f”.//section[@name='{section}’]”) if section_elem is None: return default item = section_elem.find(f”item[@key='{key}’]”) if item is None: return default return item.get(‘value’) def set(self, section: str, key: str, value: str): “””设置配置值””” section_elem = self.root.find(f”.//section[@name='{section}’]”) if section_elem is None: section_elem = ET.SubElement(self.root, ‘section’) section_elem.set(‘name’, section) item = section_elem.find(f”item[@key='{key}’]”) if item is None: item = ET.SubElement(section_elem, ‘item’) item.set(‘key’, key) item.set(‘value’, str(value)) self._save() def get_database_config(self) -> dict: “””获取数据库配置””” return { ‘host’: self.get(‘database’, ‘host’, ‘localhost’), ‘port’: int(self.get(‘database’, ‘port’, ‘3306’)), ‘username’: self.get(‘database’, ‘username’, ‘root’), ‘password’: self.get(‘database’, ‘password’, ”), ‘database’: self.get(‘database’, ‘database’, ‘test’) } # 使用示例 config = ConfigManager() config.set(‘database’, ‘host’, ‘192.168.1.100’) config.set(‘database’, ‘port’, ‘5432’) config.set(‘app’, ‘debug’, ‘true’) print(config.get_database_config())
- import csv import xml.etree.ElementTree as ET from datetime import datetime def csv_to_xml(csv_file: str, xml_file: str, root_tag: str = ‘data’): “””将 CSV 文件转换为 XML””” root = ET.Element(root_tag) root.set(‘generated’, datetime.now().isoformat()) root.set(‘source’, csv_file) with open(csv_file, ‘r’, encoding=’utf-8′) as f: reader = csv.DictReader(f) for i, row in enumerate(reader, 1): record = ET.SubElement(root, ‘record’) record.set(‘id’, str(i)) for key, value in row.items(): # 清理标签名(XML 标签不能以数字开头,不能包含空格) clean_key = key.strip().replace(‘ ‘, ‘_’) if clean_key[0].isdigit(): clean_key = ‘f_’ + clean_key field = ET.SubElement(record, clean_key) field.text = value # 美化并保存 tree = ET.ElementTree(root) ET.indent(tree, space=’ ‘) tree.write(xml_file, encoding=’utf-8’, xml_declaration=True) print(f”成功转换: {csv_file} -> {xml_file}”) # 示例 CSV 内容: # name,age,city # 张三,28,北京 # 李四,32,上海 # csv_to_xml(‘data.csv’, ‘output.xml’)
- # ❌ 错误:默认编码可能不支持中文 tree.write(‘output.xml’) # ✅ 正确:指定 UTF-8 编码 tree.write(‘output.xml’, encoding=’utf-8′, xml_declaration=True)
- # 处理带命名空间的 XML xml_with_ns = “””<?xml version=”1.0″?> <root xmlns:ns=”http://example.com/ns”> <ns:item>内容</ns:item> </root>””” root = ET.fromstring(xml_with_ns) # 方法1:使用完整标签名 for elem in root.findall(‘{http://example.com/ns}item’): print(elem.text) # 方法2:定义命名空间字典 namespaces = {‘ns’: ‘http://example.com/ns’} for elem in root.findall(‘ns:item’, namespaces): print(elem.text)
- # XML 是大小写敏感的 root.find(‘Book’) # 找不到 <book> root.find(‘book’) # 正确
- # 大文件处理(>100MB) # ❌ 错误:一次性加载 tree = ET.parse(‘huge.xml’) # ✅ 正确:迭代解析 for event, elem in ET.iterparse(‘huge.xml’, events=(‘end’,)): if elem.tag == ‘record’: process(elem) elem.clear() # 释放内存
- # ET 自动处理特殊字符 root = ET.Element(‘test’) root.text = ‘<特殊内容> & “引号”‘ # 输出: <特殊内容> & "引号"
目录
- 什么是 ElementTree
- 基础概念:XML 结构
- 解析 XML 文件
- 1. 基本解析方法
- 2. 完整解析示例
- 遍历 XML 树
- 1. 迭代遍历(内存友好)
- 2. 按层级遍历
- 查找元素
- 获取元素数据
- 完整的数据提取工具类
- 修改 XML
- 1. 修改现有元素
- 2. 批量修改工具
- 创建 XML
- 1. 从零创建 XML
- 2. 使用 Element 工厂函数
- 实际应用案例
- 案例1:配置文件管理器
- 案例2:数据转换器(CSV 转 XML)
- 常见错误与解决方案
- 1. 编码问题
- 2. 命名空间处理
- 3. 大小写敏感
- 4. 内存优化
- 5. 特殊字符转义
- 总结
xml.etree.ElementTree(通常简称为 ET)是 Python 标准库中用于解析和创建 XML 数据的模块。它提供了轻量级、高效的 API,适合处理小到中等规模的 XML 文档。
为什么选择 ET?
- ✅ Python 内置,无需安装
- ✅ 简单易用的 API 设计
- ✅ 内存效率高(迭代解析)
- ✅ 支持 XPath 表达式查找元素
在深入学习之前,先了解 XML 的基本结构:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 这是注释 -->
<library> <!-- 根元素 -->
<book id="001"> <!-- 元素 + 属性 -->
<title>Python编程</title> <!-- 子元素 -->
<author>张三</author>
<price>59.00</price>
</book>
</library>
核心概念:
- Element(元素):XML 标签,如
<book> - Tag(标签名):元素的名称,如
"book" - Attribute(属性):元素的特征,如
id="001" - Text(文本内容):元素内的文本,如
"Python编程" - Tail(尾部文本):结束标签后的文本(较少使用)
ET 提供了两种主要解析方式:
import xml.etree.ElementTree as ET
# 方法1:解析文件(推荐用于文件)
tree = ET.parse('books.xml') # 返回 ElementTree 对象
root = tree.getroot() # 获取根元素
# 方法2:解析字符串(用于从网络或内存读取)
xml_string = """<?xml version="1.0"?>
<library>
<book id="001">
<title>Python编程</title>
</book>
</library>"""
root = ET.fromstring(xml_string) # 直接返回根元素
假设我们有如下 books.xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<library location="北京">
<book id="001" category="编程">
<title>Python编程:从入门到实践</title>
<author>埃里克·马瑟斯</author>
<price currency="CNY">89.00</price>
<publish_date>2020-05</publish_date>
</book>
<book id="002" category="小说">
<title>百年孤独</title>
<author>加西亚·马尔克斯</author>
<price currency="CNY">55.00</price>
<publish_date>2011-06</publish_date>
</book>
<book id="003" category="编程">
<title>流畅的Python</title>
<author>卢西亚诺·拉马略</author>
<price currency="CNY">139.00</price>
<publish_date>2017-05</publish_date>
</book>
</library>
解析代码:
import xml.etree.ElementTree as ET
def parse_library():
"""解析图书馆 XML 文件"""
try:
# 解析 XML 文件
tree = ET.parse('books.xml')
root = tree.getroot()
print(f"根元素标签: {root.tag}")
print(f"根元素属性: {root.attrib}")
print(f"子元素数量: {len(root)}")
print("-" * 50)
# 遍历所有 book 元素
for book in root.findall('book'):
book_id = book.get('id') # 获取属性
category = book.get('category')
title = book.find('title').text # 获取文本内容
author = book.find('author').text
price_elem = book.find('price')
price = price_elem.text
currency = price_elem.get('currency')
print(f"图书ID: {book_id}")
print(f"类别: {category}")
print(f"书名: {title}")
print(f"作者: {author}")
print(f"价格: {currency} {price}")
print("-" * 50)
except ET.ParseError as e:
print(f"XML 解析错误: {e}")
except FileNotFoundError:
print("文件未找到,请确保 books.xml 存在")
if __name__ == "__main__":
parse_library()
输出结果:
根元素标签: library
根元素属性: {'location': '北京'}
子元素数量: 3
————————————————–
图书ID: 001
类别: 编程
书名: Python编程:从入门到实践
作者: 埃里克·马瑟斯
价格: CNY 89.00
————————————————–
…
对于大型 XML 文件,使用迭代器避免一次性加载所有数据:
import xml.etree.ElementTree as ET
def iterate_xml():
"""使用迭代器遍历大型 XML"""
# iterparse 在解析时生成事件,适合大文件
context = ET.iterparse('books.xml', events=('start', 'end'))
context = iter(context)
event, root = next(context)
book_count = 0
for event, elem in context:
# 'start' 事件:元素开始
# 'end' 事件:元素结束(此时元素已完整)
if event == 'end' and elem.tag == 'book':
book_count += 1
title = elem.find('title').text
print(f"处理第 {book_count} 本书: {title}")
# 处理完后清除元素释放内存
elem.clear()
root.clear()
print(f"总共处理了 {book_count} 本书")
# 递归遍历所有元素
def recursive_walk(element, level=0):
"""递归打印 XML 结构"""
indent = " " * level
print(f"{indent}<{element.tag}> {element.attrib if element.attrib else ''}")
if element.text and element.text.strip():
print(f"{indent} 文本: {element.text.strip()}")
for child in element:
recursive_walk(child, level + 1)
print(f"{indent}</{element.tag}>")
# 使用示例
tree = ET.parse('books.xml')
recursive_walk(tree.getroot())
def traverse_by_level(root):
"""按层级遍历(广度优先)"""
from collections import deque
queue = deque([(root, 0)])
while queue:
elem, level = queue.popleft()
indent = " " * level
print(f"{indent}[{level}] {elem.tag}: {elem.attrib}")
for child in elem:
queue.append((child, level + 1))
traverse_by_level(tree.getroot())
ET 支持有限的 XPath 表达式,非常实用:
import xml.etree.ElementTree as ET
tree = ET.parse('books.xml')
root = tree.getroot()
# 1. find() - 查找第一个匹配的直接子元素
first_book = root.find('book')
print(f"第一本书: {first_book.find('title').text}")
# 2. findall() - 查找所有匹配的直接子元素
all_books = root.findall('book')
print(f"图书总数: {len(all_books)}")
# 3. iter() - 递归查找所有指定标签
all_prices = root.iter('price')
print("所有价格:")
for price in all_prices:
print(f" {price.text} {price.get('currency')}")
# 4. 带条件的查找(XPath 语法)
# 查找 category="编程" 的所有图书
programming_books = root.findall(".//book[@category='编程']")
print(f"n编程类图书数量: {len(programming_books)}")
# 5. 复杂 XPath 示例
# 查找价格大于 100 的图书(需要遍历判断)
expensive_books = []
for book in root.findall('book'):
price = float(book.find('price').text)
if price > 100:
expensive_books.append(book.find('title').text)
print(f"高价图书: {expensive_books}")
# 6. 查找特定路径
# 查找第一个 book 下的 title
title = root.find('./book[1]/title')
print(f"第一本书书名: {title.text}")
支持的 XPath 语法:
| 语法 | 说明 |
|---|---|
tag |
选择直接子元素 |
* |
匹配所有子元素 |
. |
当前元素 |
// |
递归查找所有后代 |
.. |
父元素 |
[@attrib] |
有某属性的元素 |
[@attrib='value'] |
属性等于某值的元素 |
[tag] |
有某子元素的元素 |
[position] |
第 N 个元素(从1开始) |
import xml.etree.ElementTree as ET
from dataclasses import dataclass
from typing import List, Optional, Dict
@dataclass
class Book:
"""图书数据类"""
id: str
category: str
title: str
author: str
price: float
currency: str
publish_date: str
class XMLExtractor:
"""XML 数据提取器"""
def __init__(self, xml_file: str):
self.tree = ET.parse(xml_file)
self.root = self.tree.getroot()
def get_root_info(self) -> Dict:
"""获取根元素信息"""
return {
'tag': self.root.tag,
'attributes': dict(self.root.attrib),
'children_count': len(self.root)
}
def extract_all_books(self) -> List[Book]:
"""提取所有图书信息"""
books = []
for book_elem in self.root.findall('book'):
book = Book(
id=book_elem.get('id', ''),
category=book_elem.get('category', ''),
title=self._get_text(book_elem, 'title'),
author=self._get_text(book_elem, 'author'),
price=float(self._get_text(book_elem, 'price')),
currency=book_elem.find('price').get('currency', 'CNY'),
publish_date=self._get_text(book_elem, 'publish_date')
)
books.append(book)
return books
def _get_text(self, parent: ET.Element, tag: str, default: str = '') -> str:
"""安全获取子元素文本"""
elem = parent.find(tag)
return elem.text if elem is not None else default
def get_books_by_category(self, category: str) -> List[Dict]:
"""按类别筛选图书"""
results = []
xpath = f".//book[@category='{category}']"
for book in self.root.findall(xpath):
results.append({
'id': book.get('id'),
'title': book.find('title').text,
'author': book.find('author').text
})
return results
def get_statistics(self) -> Dict:
"""获取统计信息"""
books = self.extract_all_books()
if not books:
return {}
prices = [b.price for b in books]
categories = {}
for b in books:
categories[b.category] = categories.get(b.category, 0) + 1
return {
'total_books': len(books),
'avg_price': sum(prices) / len(prices),
'max_price': max(prices),
'min_price': min(prices),
'categories': categories
}
# 使用示例
if __name__ == "__main__":
extractor = XMLExtractor('books.xml')
print("=== 根元素信息 ===")
print(extractor.get_root_info())
print("n=== 所有图书 ===")
for book in extractor.extract_all_books():
print(f"{book.id}: {book.title} ({book.author}) - {book.currency}{book.price}")
print("n=== 编程类图书 ===")
print(extractor.get_books_by_category('编程'))
print("n=== 统计信息 ===")
stats = extractor.get_statistics()
print(f"图书总数: {stats['total_books']}")
print(f"平均价格: ¥{stats['avg_price']:.2f}")
print(f"价格区间: ¥{stats['min_price']:.2f} - ¥{stats['max_price']:.2f}")
print(f"类别分布: {stats['categories']}")
import xml.etree.ElementTree as ET
def modify_xml():
"""修改 XML 内容"""
tree = ET.parse('books.xml')
root = tree.getroot()
# 1. 修改属性
root.set('updated', '2024-01-01')
root.set('location', '上海') # 修改现有属性
# 2. 修改元素文本
for book in root.findall('book'):
price_elem = book.find('price')
old_price = float(price_elem.text)
# 打 8 折
new_price = old_price * 0.8
price_elem.text = f"{new_price:.2f}"
price_elem.set('discount', '0.8')
# 3. 添加新元素
for book in root.findall('book'):
stock = ET.SubElement(book, 'stock')
stock.text = '100'
stock.set('warehouse', 'A1')
# 4. 删除元素
# 删除第一本书的 publish_date
first_book = root.find('book')
publish_date = first_book.find('publish_date')
if publish_date is not None:
first_book.remove(publish_date)
# 5. 保存修改
tree.write('books_modified.xml',
encoding='utf-8',
xml_declaration=True,
short_empty_elements=False)
print("修改完成,已保存到 books_modified.xml")
modify_xml()
class XMLModifier:
"""XML 批量修改器"""
def __init__(self, input_file: str):
self.tree = ET.parse(input_file)
self.root = self.tree.getroot()
self.modified = False
def update_prices(self, increase_rate: float = 0.1):
"""批量更新价格"""
for price_elem in self.root.iter('price'):
old_price = float(price_elem.text)
new_price = old_price * (1 + increase_rate)
price_elem.text = f"{new_price:.2f}"
price_elem.set('updated', 'true')
self.modified = True
print(f"已更新所有价格,涨幅 {increase_rate*100}%")
def add_element_to_all(self, tag: str, text: str, attrib: dict = None):
"""为所有 book 添加子元素"""
attrib = attrib or {}
for book in self.root.findall('book'):
elem = ET.SubElement(book, tag, attrib)
elem.text = text
self.modified = True
print(f"已为所有图书添加 <{tag}> 元素")
def remove_element_by_tag(self, tag: str):
"""删除所有指定标签的元素"""
count = 0
for parent in self.root.iter():
for child in list(parent): # 使用 list 避免迭代时修改
if child.tag == tag:
parent.remove(child)
count += 1
self.modified = True
print(f"已删除 {count} 个 <{tag}> 元素")
def save(self, output_file: str = None):
"""保存文件"""
if not self.modified:
print("没有修改需要保存")
return
output = output_file or 'modified.xml'
self.tree.write(output,
encoding='utf-8',
xml_declaration=True)
print(f"已保存到: {output}")
# 使用示例
modifier = XMLModifier('books.xml')
modifier.update_prices(0.15) # 涨价 15%
modifier.add_element_to_all('status', '在售', {'available': 'true'})
modifier.save('books_updated.xml')
import xml.etree.ElementTree as ET
from datetime import datetime
def create_library_xml():
"""创建图书馆 XML 文件"""
# 创建根元素
root = ET.Element('library')
root.set('version', '1.0')
root.set('created', datetime.now().isoformat())
# 添加注释
comment = ET.Comment(' 这是一个自动生成的图书馆数据文件 ')
root.append(comment)
# 创建图书数据
books_data = [
{
'id': '004',
'category': '科幻',
'title': '三体',
'author': '刘慈欣',
'price': '98.00',
'currency': 'CNY',
'tags': ['雨果奖', '科幻经典', '系列作品']
},
{
'id': '005',
'category': '技术',
'title': '深度学习',
'author': '伊恩·古德费洛',
'price': '168.00',
'currency': 'CNY',
'tags': ['AI', '机器学习', '教材']
}
]
for book_data in books_data:
# 创建 book 元素
book = ET.SubElement(root, 'book')
book.set('id', book_data['id'])
book.set('category', book_data['category'])
# 添加子元素
title = ET.SubElement(book, 'title')
title.text = book_data['title']
author = ET.SubElement(book, 'author')
author.text = book_data['author']
price = ET.SubElement(book, 'price')
price.text = book_data['price']
price.set('currency', book_data['currency'])
# 添加标签列表
tags = ET.SubElement(book, 'tags')
for tag_text in book_data['tags']:
tag = ET.SubElement(tags, 'tag')
tag.text = tag_text
# 创建 ElementTree 对象
tree = ET.ElementTree(root)
# 美化输出(缩进)
ET.indent(tree, space=' ', level=0)
# 保存到文件
tree.write('new_library.xml',
encoding='utf-8',
xml_declaration=True,
short_empty_elements=False)
print("成功创建 new_library.xml")
# 同时返回字符串形式
return ET.tostring(root, encoding='unicode')
xml_string = create_library_xml()
print("n生成的 XML 内容:")
print(xml_string)
生成的 XML 结构:
<?xml version='1.0' encoding='utf-8'?>
<library created="2024-01-15T10:30:00" version="1.0">
<!-- 这是一个自动生成的图书馆数据文件 -->
<book category="科幻" id="004">
<title>三体</title>
<author>刘慈欣</author>
<price currency="CNY">98.00</price>
<tags>
<tag>雨果奖</tag>
<tag>科幻经典</tag>
<tag>系列作品</tag>
</tags>
</book>
...
</library>
def create_element_factory():
"""使用工厂模式创建 XML"""
def create_book(id, title, author, **kwargs):
"""创建图书元素的工厂函数"""
book = ET.Element('book', {'id': id})
ET.SubElement(book, 'title').text = title
ET.SubElement(book, 'author').text = author
for key, value in kwargs.items():
if isinstance(value, dict):
# 带属性的元素
elem = ET.SubElement(book, key, value.get('attrib', {}))
elem.text = value.get('text', '')
else:
ET.SubElement(book, key).text = str(value)
return book
# 构建 XML
root = ET.Element('catalog')
book1 = create_book(
'006',
'Python数据科学手册',
'杰克·万托布拉斯',
price={'text': '128.00', 'attrib': {'currency': 'CNY'}},
publisher='人民邮电出版社',
year='2020'
)
root.append(book1)
# 保存
tree = ET.ElementTree(root)
ET.indent(tree, space=' ')
tree.write('catalog.xml', encoding='utf-8', xml_declaration=True)
print("创建 catalog.xml 成功")
create_element_factory()
import xml.etree.ElementTree as ET
import os
class ConfigManager:
"""XML 配置文件管理器"""
CONFIG_FILE = 'app_config.xml'
def __init__(self):
self.tree = None
self.root = None
self._load_or_create()
def _load_or_create(self):
"""加载或创建配置文件"""
if os.path.exists(self.CONFIG_FILE):
self.tree = ET.parse(self.CONFIG_FILE)
self.root = self.tree.getroot()
else:
self.root = ET.Element('configuration')
self.root.set('version', '1.0')
self.tree = ET.ElementTree(self.root)
self._save()
def _save(self):
"""保存配置"""
ET.indent(self.tree, space=' ')
self.tree.write(self.CONFIG_FILE, encoding='utf-8', xml_declaration=True)
def get(self, section: str, key: str, default=None):
"""获取配置值"""
section_elem = self.root.find(f".//section[@name='{section}']")
if section_elem is None:
return default
item = section_elem.find(f"item[@key='{key}']")
if item is None:
return default
return item.get('value')
def set(self, section: str, key: str, value: str):
"""设置配置值"""
section_elem = self.root.find(f".//section[@name='{section}']")
if section_elem is None:
section_elem = ET.SubElement(self.root, 'section')
section_elem.set('name', section)
item = section_elem.find(f"item[@key='{key}']")
if item is None:
item = ET.SubElement(section_elem, 'item')
item.set('key', key)
item.set('value', str(value))
self._save()
def get_database_config(self) -> dict:
"""获取数据库配置"""
return {
'host': self.get('database', 'host', 'localhost'),
'port': int(self.get('database', 'port', '3306')),
'username': self.get('database', 'username', 'root'),
'password': self.get('database', 'password', ''),
'database': self.get('database', 'database', 'test')
}
# 使用示例
config = ConfigManager()
config.set('database', 'host', '192.168.1.100')
config.set('database', 'port', '5432')
config.set('app', 'debug', 'true')
print(config.get_database_config())
import csv
import xml.etree.ElementTree as ET
from datetime import datetime
def csv_to_xml(csv_file: str, xml_file: str, root_tag: str = 'data'):
"""将 CSV 文件转换为 XML"""
root = ET.Element(root_tag)
root.set('generated', datetime.now().isoformat())
root.set('source', csv_file)
with open(csv_file, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for i, row in enumerate(reader, 1):
record = ET.SubElement(root, 'record')
record.set('id', str(i))
for key, value in row.items():
# 清理标签名(XML 标签不能以数字开头,不能包含空格)
clean_key = key.strip().replace(' ', '_')
if clean_key[0].isdigit():
clean_key = 'f_' + clean_key
field = ET.SubElement(record, clean_key)
field.text = value
# 美化并保存
tree = ET.ElementTree(root)
ET.indent(tree, space=' ')
tree.write(xml_file, encoding='utf-8', xml_declaration=True)
print(f"成功转换: {csv_file} -> {xml_file}")
# 示例 CSV 内容:
# name,age,city
# 张三,28,北京
# 李四,32,上海
# csv_to_xml('data.csv', 'output.xml')
# ❌ 错误:默认编码可能不支持中文
tree.write('output.xml')
# ✅ 正确:指定 UTF-8 编码
tree.write('output.xml', encoding='utf-8', xml_declaration=True)
# 处理带命名空间的 XML
xml_with_ns = """<?xml version="1.0"?>
<root xmlns:ns="http://example.com/ns">
<ns:item>内容</ns:item>
</root>"""
root = ET.fromstring(xml_with_ns)
# 方法1:使用完整标签名
for elem in root.findall('{http://example.com/ns}item'):
print(elem.text)
# 方法2:定义命名空间字典
namespaces = {'ns': 'http://example.com/ns'}
for elem in root.findall('ns:item', namespaces):
print(elem.text)
# XML 是大小写敏感的
root.find('Book') # 找不到 <book>
root.find('book') # 正确
# 大文件处理(>100MB)
# ❌ 错误:一次性加载
tree = ET.parse('huge.xml')
# ✅ 正确:迭代解析
for event, elem in ET.iterparse('huge.xml', events=('end',)):
if elem.tag == 'record':
process(elem)
elem.clear() # 释放内存
# ET 自动处理特殊字符
root = ET.Element('test')
root.text = '<特殊内容> & "引号"'
# 输出: <特殊内容> & "引号"
| 功能 | 方法 | 说明 |
|---|---|---|
| 解析文件 | ET.parse() |
返回 ElementTree |
| 解析字符串 | ET.fromstring() |
返回根元素 |
| 查找单个 | element.find() |
第一个匹配 |
| 查找多个 | element.findall() |
所有直接子元素 |
| 递归查找 | element.iter() |
所有后代元素 |
| 获取属性 | element.get() |
获取属性值 |
| 获取文本 | element.text |
元素内容 |
| 创建子元素 | ET.SubElement() |
工厂函数 |
| 保存文件 | tree.write() |
写入文件 |
最佳实践建议:
- 始终指定
encoding='utf-8'保存中文 - 大文件使用
iterparse()迭代处理 - 使用
try-except捕获ParseError - 复杂查询考虑使用
lxml库(支持完整 XPath) - 修改前先备份原文件
通过本教程的学习,您已经掌握了 Python ET 模块的核心功能,可以处理绝大多数 XML 数据操作需求。建议动手实践每个示例代码,加深理解。
到此这篇关于Python ET.parse 模块功能详解的文章就介绍到这了,更多相关Python ET.parse内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!
您可能感兴趣的文章:
- Python参数解析模块sys、getopt、argparse使用与对比分析
- python中argparse模块用法详解(小白入门)
- 从零开始用Python解析命令行参数之标准库argparse和第三方库click
- 深入解析 Python 的 argparse命令行参数的实战案例
- Python中argparse的使用小结