我对编码提出了挑战,所以这可能很简单,但我被困住了。
我试图解析一个通过电子邮件发送到App引擎的新的接收邮件功能的XML文件。首先,我将该XML粘贴到消息正文中,它使用CElementTree进行了很好的解析。然后我改为使用附件,用CElementTree解析它会产生以下错误:
SyntaxError:格式不正确(无效令牌):第3行,第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在第三行。
发布于 2009-10-21 05:10:42
啊,算了。App中有一个bug,当您对所有附件进行解码时,它会对所有附件调用lower()。这使得CDATA字符串无效。
下面是一个指向bug报告的链接:http://code.google.com/p/googleappengine/issues/detail?id=2289#c2
https://stackoverflow.com/questions/1597604
复制相似问题