我收到此代码错误!
首先,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/