我想用lxml處理一些數據。它工作正常,我的開發服務器上,但對生產下列代碼:生產lxml編碼錯誤
parser = etree.XMLParser(encoding='cp1251')
拋出:
File "parser.pxi", line 1288, in lxml.etree.XMLParser.__init__ (third_party/apphosting/python/lxml/src/lxml/lxml.etree.c:77726)
File "parser.pxi", line 738, in lxml.etree._BaseParser.__init__ (third_party/apphosting/python/lxml/src/lxml/lxml.etree.c:73404)
LookupError: unknown encoding: 'cp1251'
我使用LXML 2.3。 GAE似乎支持相同的版本。那麼爲什麼這個錯誤呢?
編輯:
我指定不同的編碼,以XMLParser
,如CP1252,ISO-8859-5,ISO-8859-2,它總是扔在GAE相同的錯誤,但我工作的本地機器上。這些都是流行的編碼,而且GAE上的lxml必須支持它們。我相信這在GAE上的lxml構建中是錯誤的。
我創建了一個問題:http://code.google.com/p/googleappengine/issues/detail?id=7315
EDIT2:
完全回溯:
unknown encoding: 'cp1251'
Traceback (most recent call last):
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~my_cool_app_id/1.358126884781269352/main.py", line 29, in get
parser = etree.XMLParser(encoding='cp1251')
File "parser.pxi", line 1288, in lxml.etree.XMLParser.__init__ (third_party/apphosting/python/lxml/src/lxml/lxml.etree.c:77726)
File "parser.pxi", line 738, in lxml.etree._BaseParser.__init__ (third_party/apphosting/python/lxml/src/lxml/lxml.etree.c:73404)
LookupError: unknown encoding: 'cp1251'
這適用於我在shell-27.appspot.com(一旦我注意不要讓解析器被酸洗)。你使用Python 2.7嗎?你可以包含完整的堆棧跟蹤嗎? – 2012-04-11 04:19:07
另外,你有沒有嘗試過之前解碼文本傳遞給XML解析器? – 2012-04-11 04:19:39
@NickJohnson感謝您的意見!是的,我正在使用Python 2.7。在shell-27.appspot.com我甚至不能導入lxml(沒有名爲lxml的模塊)。我能夠用'my_xml_str.decode('cp1251')'解碼文本,但這不是最理想的,因爲在將這樣的unicode字符串傳遞給'etree.XML()'進行解析之前,我必須手動刪除xml中的編碼聲明證明自己。其次,xml文檔的大小爲5Mb,這比允許lxml自行解碼字符串要慢。我編輯了我的問題,包括完整的追溯。 – Maxim 2012-04-11 06:47:55