Python ';文件';对象没有属性'__获取项目';还有一个丢失的模块
我的代码有错误!! 首先,xreadlines模块不再存在,因此我对该行进行了注释。现在,当我运行此代码时,它会给出错误:Python ';文件';对象没有属性'__获取项目';还有一个丢失的模块,python,python-2.7,compiler-errors,runtime-error,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/testproject
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__'
不能在迭代器上使用订阅(这是
file.xreadlines()
返回的内容)。如果需要随机访问不同的行,只需使用file.readlines()
或list(file)
请注意,
file.xreadlines()
自Python 2.3(13年前发布!)以来一直被弃用;改为在文件对象上使用迭代(顺便说一句,这是list(file)
所做的)。如果最初没有成功,请再次执行相同的操作?只需使用fileobj.readlines()
。
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")