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

回答