Python:TypeError:'file'对象没有属性'__getitem__' [英] Python: TypeError: 'file' object has no attribute '__getitem__'

查看:195
本文介绍了Python:TypeError:'file'对象没有属性'__getitem__'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.gpx文件在文件的中间被切断。当我尝试使用 gpxpy库进行解析时,遇到以下错误。

I have a .gpx file which is cut off int the middle of the file. When I try to parse it using the gpxpy library I run into the following error.

Parsing points in track.gpx
ERROR:root:expected '>', line 3125, column 29
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gpxpy-0.8.7-py2.7.egg/gpxpy/parser.py", line 209, in parse
    self.xml_parser = LXMLParser(self.xml)
  File "/usr/local/lib/python2.7/dist-packages/gpxpy-0.8.7-py2.7.egg/gpxpy/parser.py", line 107, in __init__
    self.dom = mod_etree.XML(self.xml)
  File "lxml.etree.pyx", line 2734, in lxml.etree.XML (src/lxml/lxml.etree.c:54411)
  File "parser.pxi", line 1578, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:82748)
  File "parser.pxi", line 1457, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:81546)
  File "parser.pxi", line 965, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:78216)
  File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74472)
  File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75363)
  File "parser.pxi", line 590, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74696)
XMLSyntaxError: expected '>', line 3125, column 29

File "gpxscript.py", line 370, in extractpoints gpx = gpxpy.parse(file)
File "/usr/local/lib/python2.7/dist-packages/gpxpy-0.8.7-py2.7.egg/gpxpy/__init__.py",
     line 28, in parse raise mod_gpx.GPXException('Error parsing {0}: {1}'
                       .format(xml_or_file[0 : 100], parser.get_error()))
TypeError: 'file' object has no attribute '__getitem__'

这些脚本产生错误的相关命令。

These are the relevant commands of the script which produces the error.

368  file = open(filepath)
369  try:
370      gpx = gpxpy.parse(file)
371  except gpxpy.gpx.GPXException:
372      print "GPXException for %s." % filepath
373      return 1






I根据建议,提交了图书馆的错误。我将一个示例文件添加到产生语法错误的错误报告中。


I filed a bug for the library as suggested. I added a sample file to the bug report which produces the syntax error.

推荐答案

这似乎是 gpxpy 的错误处理。

查看源代码 parse ,当解析器失败而不引发异常时尝试通过以下方式引发异常:

Looking at the source to parse, when the parser fails without raising an exception, it tries to raise an exception with this:

raise mod_gpx.GPXException('Error parsing {0}: {1}'.format(xml_or_file[0 : 100], parser.get_error()))

这假设 xml_or_file 是一个XML字符串 - 但是,顾名思义,它允许是字符串或文件对象。所以,你正在做的(给它一个文件对象)是完全合法的,它应该是有效的,因此它不是一个bug。

This assumes that xml_or_file is an XML string—but, as the name implies, it's allowed to be either a string or a file object. So, what you're doing (giving it a file object) is perfectly legal and should work, and it doesn't, and therefore it's a bug.

所以,您应该提交问题。正确的补丁应该是这样的:

So, you should file an issue. The correct patch should be something like:

if not parser.is_valid():
    try:
        fragment = xml_or_file[0 : 100]
    except TypeError:
        xml_or_file.seek(0)
        fragment = xml_or_file.read(100)
    raise mod_gpx.GPXException('Error parsing {0}: {1}'.format(fragment, parser.get_error()))






那么,你如何解决这个问题?几个选项:


So, how do you work around this? A few options:


  1. 由于它只是发生在无效的文件,反正你可以使用除了异常 except(gpxpy.gpx.GPXException,TypeError)

因为只有当你给它一个文件对象时,才会给它一个字符串: gpx = gpx.parse(file.read())。这个文件是非常大的,当然是一个坏主意。

Since it only happens when you give it a the file object, give it a string instead: gpx = gpx.parse(file.read()). This is a bad idea if the file is very large, of course.

由于bug功能只包含了12行简单的代码,直接使用真实的功能。或者,如果您喜欢包装,请复制,修复它,并使用自己的副本。

Since the buggy function is only 12 lines of trivial code wrapping the real function, just use the real function directly. Or, if you like the wrapper, copy it, fix it, and use your own copy instead.






同时,由于我在这个库中看到的第一位代码有一些明显的红旗(为什么 xml_or_file [0:100] 而不是只是 xml_or_file [:100] ?为什么要捕获异常,抛出它们只是设置一个标志,然后使用该标志来引发一个新的异常,所有的信息缺少?),如果您无法自行调试图书馆,我不认为这可以让您使用。


Meanwhile, given that the very first bit of code I looked at in this library has some obvious red flags (Why xml_or_file[0 : 100] instead of just xml_or_file[:100]? Why catch exceptions, throw them away and just set a flag, and then use that flag to raise a new exception with all the information missing?), if you're not able to debug libraries on your own, I don't think this one is ready for you to use.

这篇关于Python:TypeError:'file'对象没有属性'__getitem__'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆