【发布时间】:2009-10-20 22:07:19
【问题描述】:
我在编码方面遇到了挑战,所以这可能很简单,但我被卡住了。
我正在尝试解析通过电子邮件发送到App Engine 的新接收邮件功能的 XML 文件。首先,我只是将 XML 粘贴到消息的正文中,并使用 CElementTree 很好地解析它。然后我改为使用附件,并用 CElementTree 解析它会产生这个错误:
SyntaxError: not well-formed (invalid token): line 3, column 10
我已经从正文中的电子邮件和附件中输出了 XML,它们在我看来是一样的。我假设将其粘贴到框中会以附加文件的方式更改编码,但我不知道如何修复它。
前几行如下所示:
<?xml version="1.0" standalone="yes"?>
<gpx xmlns="http://www.topografix.com/GPX/1/0" version="1.0" creator="TopoFusion 2.85" xmlns:TopoFusion="http://www.TopoFusion.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.TopoFusion.com http://www.TopoFusion.com/topofusion.xsd">
<name><![CDATA[Pacific Crest Trail section K hike 4]]></name><desc><![CDATA[Pacific Crest Trail section K hike 4. Five Lakes to Old Highway 40 near Donner. As described in Day Hikes on the PCT California edition by George & Patricia Semb. See pages 150-152 for access and exit trailheads. GPS data provided by the USFS]]></desc><author><![CDATA[MikeOnTheTrail]]></author><email><![CDATA[michaelonthetrail@yahoo.com]]></email><url><![CDATA[http://www.pcta.org]]></url>
<urlname><![CDATA[Pacific Crest Trail Association Homepage]]></urlname>
<time>2006-07-08T02:16:05Z</time>
已编辑以添加更多信息:
我有一个几千行的GPX 文件。如果我将其粘贴到消息正文中,我可以正确解析它,如下所示:
gpxcontent = message.bodies(content_type='text/plain')
for x in gpxcontent:
gpxcontent = x[1].decode()
for event, elem in ET.iterparse(StringIO.StringIO(gpxcontent), events=("start", "start-ns")):
如果我将其作为附件附加到邮件中,请使用 Gmail。然后像这样提取它:
if isinstance(message.attachments, tuple):
attachments = [message.attachments]
gpxcontent = attachments[0][3].decode()
for event, elem in ET.iterparse(StringIO.StringIO(gpxcontent), events=("start", "start-ns")):
我收到上面的错误。第 3 行第 10 列似乎是 ![CDATA 在第三行的开始。
【问题讨论】:
-
请给我们更多数据。您提供的示例在添加 后已被我的 cElementTree 成功解析。
-
你说是编码问题,但是报错信息是
not well-formed (invalid token),和编码无关。请详细说明“我改为使用附件”——你究竟传递给 cElementTree 什么,以及如何传递?显示代码。 “第 3 行,第 10 列”到底是什么?