2009-10-20 45 views
0

我编码质疑,所以这可能很简单,但我卡住了。cElementTree无效编码问题

我试图解析通过电子邮件发送到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[[email protected]]]></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第三行的开始。

+1

请给我们更多的数据。您提供的示例在添加之后,我的cElementTree已成功解析。 – 2009-10-20 22:40:55

+0

你说有一个编码问题,但是错误信息是'没有格式良好(无效标记)',这与编码无关。请详细说明“我更改为使用附件” - 您传递给cElementTree的具体内容如何?显示代码。 “第3行第10列”究竟是什么? – 2009-10-21 00:42:18

回答