python - "file"对象没有属性 '__getitem__' 并且缺少模块

标签 python python-2.7 compiler-errors runtime-error

我收到此代码错误!
首先,xreadlines 模块不再存在,所以我评论了这一行。现在,当我运行此代码时,它会给出错误:

Traceback (most recent call last):
  File "/home/integrand/projects/testproject2/testenv/Paragraph.py", line 58, in <module>
    show_paragraphs("/home/integrand/projects/testproject2/Files/lease.text")
  File "/home/integrand/projects/testproject2/testenv/Paragraph.py", line 50, in show_paragraphs
    for p in pp:
  File "/home/integrand/projects/testproject2/testenv/Paragraph.py", line 29, in __getitem__
    line = self.seq[self.line_num]
TypeError: 'file' object has no attribute '__getitem__' 
class Paragraphs:

    def __init__(self, fileobj, separator='\n'):

        # Ensure that we get a line-reading sequence in the best way possible:
       # import xreadlines
        try:
            # Check if the file-like object has an xreadlines method
            self.seq = fileobj.xreadlines()
        except AttributeError:
            # No, so fall back to the xreadlines module's implementation
            self.seq = fileobj.xreadlines()

        self.line_num = 0    # current index into self.seq (line number)
        self.para_num = 0    # current index into self (paragraph number)

        # Ensure that separator string includes a line-end character at the end
        if separator[-1:] != '\n': separator += '\n'
        self.separator = separator


    def __getitem__(self, index):
        if index != self.para_num:
            raise TypeError, "Only sequential access supported"
        self.para_num += 1
        # Start where we left off and skip 0+ separator lines
        while 1:
        # Propagate IndexError, if any, since we're finished if it occurs
            line = self.seq[self.line_num]
            self.line_num += 1
            if line != self.separator: break
        # Accumulate 1+ nonempty lines into result
        result = [line]
        while 1:
        # Intercept IndexError, since we have one last paragraph to return
            try:
                # Let's check if there's at least one more line in self.seq
                line = self.seq[self.line_num]
            except IndexError:
                # self.seq is finished, so we exit the loop
                break
            # Increment index into self.seq for next time
            self.line_num += 1
            if line == self.separator: break
            result.append(line)
        return ''.join(result)

def show_paragraphs(filename,numpars=5):
    pp = Paragraphs(open(filename))
    for p in pp:
        print "Par#%d, line# %d: %s" % (
            pp.para_num, pp.line_num, repr(p))
        if pp.para_num>numpars: break


if __name__ == '__main__':
    #pdfparser("/home/USER/projects/testproject2/Files/Lea.pdf")
    show_paragraphs("/home/USER/projects/testproject2/Files/Lea.text")

最佳答案

您不能在迭代器上使用订阅(file.xreadlines() 返回的内容)。如果您需要随机访问不同的行,只需使用 file.readlines()list(file)反而。

请注意 file.xreadlines()自 Python 2.3(13 年前发布!)以来已被弃用;而是对文件对象使用迭代(顺便提一下,list(file) 就是这样做的)。

关于python - "file"对象没有属性 '__getitem__' 并且缺少模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38204366/

相关文章:

Python:将多维字典的列表转换为字典键,并进行异常处理

compiler-errors - 如何使用自定义MPI实现编译OpenFoam?

c - Flex 中 '(' 标记之前的预期标识符或 '{'

python - Python 中的列表推导式,在迭代之间具有可变状态

python - TypeError : float() argument must be a string or a number, 不是 'NoneType'

python - 如何使用 pytest 对 python datetime.datetime.now 进行猴子补丁?

python - 将 Python 安装到主目录

python - 无法将一些难以辨认的内容处理为可读

python-2.7 - 导入错误 : cannot import name corpora with Gensim

compiler-errors - 字节和位运算符