如何使用 lxml 从本地文件或 url 解析 xml?

2023-11-25

我尝试使用lxml来解析xml,但我有一个问题:

ValueError: invalid \x escape

这是我的代码:

from lxml import etree
root=etree.fromstring('C:\Users\hptphuong\Desktop\xmltest.xml')

我是 lxml 的新手。请帮我解决这个问题。 这是我的xml内容

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
   <book id="bk104">
      <author>Corets, Eva</author>
      <title>Oberon's Legacy</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
   </book>
   <book id="bk105">
      <author>Corets, Eva</author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberon's Legacy.</description>
   </book>
   <book id="bk106">
      <author>Randall, Cynthia</author>
      <title>Lover Birds</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-09-02</publish_date>
      <description>When Carla meets Paul at an ornithology 
      conference, tempers fly as feathers get ruffled.</description>
   </book>
   <book id="bk107">
      <author>Thurman, Paula</author>
      <title>Splish Splash</title>
      <genre>Romance</genre>
      <price>4.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>A deep sea diver finds true love twenty 
      thousand leagues beneath the sea.</description>
   </book>
   <book id="bk108">
      <author>Knorr, Stefan</author>
      <title>Creepy Crawlies</title>
      <genre>Horror</genre>
      <price>4.95</price>
      <publish_date>2000-12-06</publish_date>
      <description>An anthology of horror stories about roaches,
      centipedes, scorpions  and other insects.</description>
   </book>
   <book id="bk109">
      <author>Kress, Peter</author>
      <title>Paradox Lost</title>
      <genre>Science Fiction</genre>
      <price>6.95</price>
      <publish_date>2000-11-02</publish_date>
      <description>After an inadvertant trip through a Heisenberg
      Uncertainty Device, James Salway discovers the problems 
      of being quantum.</description>
   </book>
   <book id="bk110">
      <author>O'Brien, Tim</author>
      <title>Microsoft .NET: The Programming Bible</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-09</publish_date>
      <description>Microsoft's .NET initiative is explored in 
      detail in this deep programmer's reference.</description>
   </book>
   <book id="bk111">
      <author>O'Brien, Tim</author>
      <title>MSXML3: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>36.95</price>
      <publish_date>2000-12-01</publish_date>
      <description>The Microsoft MSXML3 parser is covered in 
      detail, with attention to XML DOM interfaces, XSLT processing, 
      SAX and more.</description>
   </book>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

另外,我们可以使用 lxml 从 url 解析 xml。

谢谢并致以最诚挚的问候,


您收到错误消息的原因invalid \x escape是你正在使用etree.fromstring()尝试从文件加载 XML。该函数用于直接从字符串加载 XML,并且您向其传递一个路径\ in it.

实际上,该函数正在尝试将您的文件路径解析为 XML。该路径包含\转义字符后面带有无效字符(即\n将是有效的换行符)

要从文件加载 XML,您需要使用etree.parse()函数如下:

from lxml import etree

root = etree.parse(r'C:\Users\hptphuong\Desktop\xmltest.xml')
# Print the loaded XML
print etree.tostring(root)

将文件路径传递给 Python 函数时,通常应该在字符串前面加上前缀r告诉Python不要尝试逃避\你的路径中的字符。例如c:\temp实际上会导致通过c:<tab character>emp,即\t被转换为制表符。添加r从一开始就阻止了这种情况的发生。

或者,您可以按如下方式传递路径:

path = "c:\\folder1\\folder2\\myfile.xml"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 lxml 从本地文件或 url 解析 xml? 的相关文章

随机推荐

  • 如何通过 Express 中的中间件链识别请求(通过 ID)。

    我正在使用 Node js 开发一个 RESTful 服务器 使用 Express 作为框架 暂时使用 Winston 作为记录器模块 该服务器将处理大量并发请求 并且能够使用 请求 ID 之类的内容跟踪每个特定请求的日志条目对我来说非常有
  • 是否能够忽略/禁用 vNext Build 中的第一步“获取源”?

    这是我们的情况 有时我们需要运行 vNext 构建 而不需要从 TFS 服务器提取任何源代码 但我们不想更改工作区映射 有没有简单的相关设置忽略或禁用获取源步骤在构建定义中 与任何其他任务不同 获取来源 创建新的构建定义时自动添加的任务 无
  • 可以使用构造函数克隆方法创建对象

    我一直以为clone 创建一个对象而不调用构造函数 但是 在阅读 Effective Java 时第 11 项 明智地覆盖克隆 我发现一个声明说 不调用任何构造函数 的规定太强了 A 行为良好的clone方法可以调用构造函数来创建对象 正在
  • 如何有效地找到特定宽度字符串的理想列数?

    I have n strings of different length s1 s2 sn that I want to display on a terminal in c columns The terminal has a width
  • 如何使用 PHP 从图像文件创建 PDF 文档

    使用 PHP 应用程序 我必须从一组图像生成单个 PDF 文档 实现这一目标的最佳方法是什么 我可以使用 TCPDF 库吗 你能给我一些例子吗 最简单的方法是使用 TCPDF http www tcpdf org 并将图像设置为完整背景 如
  • 控制 x 刻度日期值

    我有以下数据样本作为 x y 对 x 和 y 都是 Unix 时间戳 1354648326 1354648326 1354649456 1371775551 1354649664 1429649819 1354649667 14296440
  • python numpy 用不同的值填充矩阵对角线

    我看到一个函数numpy fill diagonal它为对角元素分配相同的值 但我想为每个对角元素分配不同的随机值 我怎样才能在 python 中做到这一点 可能正在使用 scipy 或其他库 那docs调用填充val标量是一个现有的文档错
  • 从服务器读取具有一定偏移量的文件

    如何从服务器读取以某个偏移量开头的文件 类似于wget c 我必须向服务器发送哪些标头 服务器必须支持哪些期货 您应该使用Range请求中的标头 但只有当服务器通知您它接受范围请求时 您才可以使用它Accept Ranges响应头 这是一个
  • CSS 表格单元格等宽

    我在表格容器内有不确定数量的表格单元格元素 div style display table div style display table cell div div style display table cell div div 是否有一
  • C++包含头文件问题

    我是 c c 新手 我对以下内容感到困惑 我是否应该将类声明放在自己的头文件中 并将实际实现放在另一个文件中 我是否应该放置标题
  • 如何抑制列表属性的 XML 标记

    序列化时是否可以避免列表属性标签 Serializable removed unnecessary public class Foo protected List
  • 更改选定段控件的颜色

    在我的应用程序中 我能够更改所选段控件的颜色 但是颜色会针对另一个索引而不是所选索引进行更改 我可以在索引中找到任何错误 Help me 我的代码如下 if SegmentRound selectedSegmentIndex 0 UICol
  • 指定通用参数的构造函数约束[重复]

    这个问题在这里已经有答案了 我有一个对象集合 我将其作为参数传递以创建另一种类型的对象 一对一 我在很多地方都这样做 基本上是从数据对象转换为业务对象 我想编写一个通用扩展方法来完成此任务 但我陷入困境 因为我不知道如何指定业务对象具有以数
  • 在 OS X 上安装 h5py

    我花了一天的时间试图得到h5pypython 模块工作 但没有成功 我已经安装了 HDF5 共享库 并按照我在网上找到的说明进行操作以使其正确 但它不起作用 下面是我尝试将模块导入 python 时收到的错误消息 我也尝试通过 MacPor
  • jqXHR - http-status-code-403(但状态代码为0)

    我得到状态代码 0 但它是代码 403 有人能告诉我问题是什么吗 JQUERY var jqxhr ajax url http gdata youtube com feeds api users bernd favorites alt js
  • IE9数组不支持indexOf

    基于http ie microsoft com testdrive HTML5 ECMAScript5Array Default html 我认为 IE9 支持数组中的 indexOf 但以下中断 知道为什么吗 错误信息如下 SCRIPT4
  • Laravel phpunit 没有获得正确的 url

    我已将 app url 配置值更改为正确的 url http testing local 用于本地测试 但是当我运行 phpunit 测试并尝试调用 时 它正在尝试查询http localhost而不是 app url 的值 我需要做什么才
  • 如何使用Chart.js显示折线图数据集点标签?

    我的设计要求是显示包含 5 个趋势数据集的折线图 沿着笔画线的每个数据值需要在其各自的数据点处显示数据值标签 不幸的是 我在 Charts js 中找不到满足此要求的选项 有解决方法可以帮助我吗 我也在小提琴上发布了这个 http jsfi
  • 我们如何改变SQL Server的页面大小?

    Per MSDN 在 SQL Server 中 页大小为 8 KB 这意味着 SQL Server 数据库 每兆字节有 128 页 我们如何更改页面大小 例如4 KB 或 12 KB 等 还有 是不是因为 innate硬件限制 页面大小选择
  • 如何使用 lxml 从本地文件或 url 解析 xml?

    我尝试使用lxml来解析xml 但我有一个问题 ValueError invalid x escape 这是我的代码 from lxml import etree root etree fromstring C Users hptphuon