cElementTree无效编码问题

2024-05-23 17:44:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在编码方面遇到了挑战,所以这可能很简单,但我卡住了。在

我试图解析通过电子邮件发送到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文件,有几千行。如果我将其粘贴到邮件正文中,我可以正确地解析它,如下所示:

^{pr2}$

如果我用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在第三行。在


Tags: comhttp附件粘贴www邮件xmlgpx