- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 python 解析一个巨大的 XML 文件,但收到此错误:
File "parser.py", line 6, in <module>
event, root = text.next()
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1281, in next
self._root = self._parser.close()
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1654, in close
self._raiseerror(v)
File "C:\Python27\lib\xml\etree\ElementTree.py", line 1506, in _raiseerror
raise err
xml.etree.ElementTree.ParseError: syntax error: line 1, column 0
我的代码现在看起来像这样
import xml.etree.ElementTree as ET
from StringIO import StringIO
text = ET.iterparse(StringIO('Posts.xml'), events=('start', 'end', 'start-ns', 'end-ns'))
text = iter(text)
event, root = text.next()
for event, elem in text:
currId = elem.get('PostTypeId')
if (currId != '1'):
root.remove(elem)
tree.write('cut.xml')
我试图解析的 XML 文件看起来像这样:
<posts>
<row FavoriteCount="4" CommentCount="4" AnswerCount="7" Tags="<discussion><answers>" Title="Why would anyone accept an answer?" LastActivityDate="2014-04-23T09:14:37.103" LastEditDate="2010-09-03T00:42:07.733" LastEditorUserId="99" OwnerUserId="4" Body="<p>I'm looking at the questions proposed during the Area 51 process:</p> <ul> <li>My supervisor thinks that all <code>If</code> statements should include <code>else</code> statements. Do you agree?</li> <li>What are common mistakes in Software Development?</li> <li>Tabs vs. Spaces: What is the one proper indentation character for everything, in every situation, ever?</li> <li>What programming language should I teach to my 4 year old son?</li> <li>What was the turning point of your programming career?</li> </ul> <p>None of these have an answer that should be accepted. The questions are interesting, and the answers would also be informative if the answer was well written and explained why the answerer thinks his method or idea is better. But I can't really see being able to accept an answer to any of these questions.</p> <p>So, if I ask a question, how do I decide if or how to accept an answer? There is no right or wrong answer and just because it works for me doesn't mean I should be floating that answer to the top - unless I'm overlooking something, the questions that are on topic here are very subjective. On Stack Overflow, there are often multiple right solutions to a problem. Here, we have a problem with an infinite number of solutions, none of which are arguably better or worse than any others.</p> <p>Thoughts?</p> " ViewCount="1582" Score="30" CreationDate="2010-09-01T19:32:45.710" PostTypeId="1" Id="1"/>
<row CommentCount="0" AnswerCount="4" Tags="<discussion><site-attributes><faq-contents><top-7>" Title="What should our FAQ contain?" LastActivityDate="2015-03-18T19:19:24.887" LastEditDate="2015-03-18T19:19:24.887" LastEditorUserId="25936" OwnerUserId="9" Body="<p>One of the big 7 questions.</p> " ViewCount="318" Score="6" CreationDate="2010-09-01T19:34:51.797" PostTypeId="1" Id="2" CommunityOwnedDate="2010-09-02T03:42:26.083"/>
<row FavoriteCount="8" CommentCount="8" AnswerCount="32" Tags="<discussion><top-7><site-attributes>" Title="What should our domain name be?" LastActivityDate="2014-04-23T09:14:37.103" LastEditDate="2010-12-20T02:46:31.950" LastEditorUserId="2314" OwnerUserId="9" Body="<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="http://meta.programmers.stackexchange.com/questions/412/write-an-elevator-pitch-tagline">Write an Elevator Pitch / Tagline</a> </p> </blockquote> <h2>Note:</h2> <p>We are closing this domain naming thread. It is asking the <em>entirely</em> wrong question. See this blog post for details: <a href="http://blog.stackoverflow.com/2010/10/domain-names-the-wrong-question/" rel="nofollow">Domain Names: Wrong Question</a> </p> <p>We're going to keep the name programmers.stackexchange.com. But we WILL be setting up redirects from the more "popular" domains names. (e.g. seasonedadvice.com to cooking.stackexchange.com, basicallymoney.com to money.stackexchange.com, and others as we go through the list).</p> <p>New question: "<strong>Write an Elevator Pitch / Tagline!</strong>"</p> <p><a href="http://meta.programmers.stackexchange.com/questions/412/write-an-elevator-pitch-tagline"><strong>Click here to contribute ideas and vote.</strong></a> </p> <p><em>[original message text below]</em></p> <p>One of the big 7 questions.</p> <ul> <li>One answer per answer please</li> <li>Only .com domain names please</li> <li>Only untaken domain names please (use whois)</li> </ul> <p>Please use <strong>lowercase characters only</strong> in domain name!<br> DomainName.com is more readable, but we have to register domainname.com!</p> " ViewCount="1146" Score="16" CreationDate="2010-09-01T19:36:08.390" PostTypeId="1" Id="3" CommunityOwnedDate="2010-09-02T03:40:00.467" ClosedDate="2010-10-08T21:02:50.313"/>
...
</posts>
最佳答案
ElementTree.iterparse
需要某种源。您要为其提供一个字符串缓冲区,其中包含内容 Posts.xml
,而不是文件 Posts.xml 的实际内容,后者显然不具有正确的 xml 文件语法。
因此,只需摆脱 StringIO
调用,ElementTree 就会为您打开文件。然而,您的输入文件还存在一些问题,导致您的文件无法正确解析(请参阅 sverasch 的答案)。
关于python - 使用 python 迭代解析一个巨大的 xml 文件但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36924383/
我通过在共享首选项中使用 GSON 将其转换为 json 来存储我的复杂对象。但是在检索它时,无法获得预期的字符串。 代码 这里 holderListCustomizationMap 是对象的复杂映射
因此,我正在尝试对大于可用RAM的gz压缩文件执行某种面向行的操作,因此排除了将其首先读取为字符串的情况。问题是,如何在rust(缺少gunzip file.gz|./my-rust-program)
我试图更好地理解为什么具有潜在大精度的大数字处理不一致,特别是在 JavaScript 及其本地化工具(例如 ECMA-402/Intl)中。我假设这与 float 的使用有关,但我想了解限制在哪里和
我们有一个 5GB 的 csv 文件,这是我们业务的主列表。 有多个类别,每个类别包含数千条记录。我们的目标是将每个类别导出为其自己的 csv 文件。 我们如何运行查询并导出数据? 运行 OSX。有没
基于上一个问题 ( see here ),我试图通过 xmlEventParse 读取许多大型 xml 文件,同时保存节点变化数据。使用此示例 xml:https://www.nlm.nih.gov/
我正在开发一个系统,它加载一个巨大的 CSV 文件(超过 100 万行)并保存到数据库中。每行也有超过一千个字段。 CSV 文件被视为一个批处理,每一行都被视为其子对象。在添加对象的过程中,每个对象都
借助node-google模块 我编写了一个简单的 Node 模块来为我的网络应用程序启用“文本网络搜索”功能,并在我的一个 View 中显示结果。 由于在来自同一 IP 的少量查询后 Google
我有相当大的 4D 阵列 [20x20x40x15000],我使用 h5py 将其作为 HDF5 文件保存到磁盘.现在的问题是我想计算整个数组的平均值,即使用: numpy.average(HDF5_
我在遗留代码库中连接巨大的 CString 时遇到问题。 CStrings 可以包含 base64 编码的文件,因此可能很大。在某些时候,这些 CString 会像这样连接起来: result +=
我正在尝试让我的服务器提供来自另一台服务器的巨大文件。但是,为了保护我的凭据免受该远程服务器的攻击,我不能简单地将请求者重定向到文件 url;另一方面,虽然使用 StreamingHttpRespon
感谢对此的任何见解,我有 2 个问题: 1) 弄清楚为什么我的本地数据库 oplog 庞大且不断增长 2) 安全删除(或重置)我的 local.oplog 以释放 18 GB 的浪费空间 场景:我一直
我的预期任务:获取大量数据(1 GB 及更多大小)json 字符串,操作(进行一些格式化、解析 json、重组 json 数据)并写入新格式化的 json 字符串作为响应。处理这种情况的更好方法是什么
我做了一个小的 Angular 4 应用程序,但我不知道如何应用 tree shaking 和 aot 编译。我运行的命令如下: ng build --prod --aot 但我得到的结果仍然很大,供
我是一名优秀的程序员,十分优秀!