如何解决pdfplumber给fp.seekposAttributeError:'dict'对象没有属性'seek'
这是我的代码:
def main():
import combinedparser as cp
from tkinter.filedialog import askopenfilenames
files = askopenfilenames()
print(files) #this gives the right files as a list of strings composed of path+filename
def file_discriminator(func):
def wrapper():
results = []
for item in files:
if item.endswith('.pdf'):
print(item + 'is pdf')
func = f1(file = item)
results.append(item,Specimen_Output)
else:
print(item + 'is text')
func = f2(file = item)
results.append(item,Specimen_Output)
return wrapper
@file_discriminator
def parse_me(**functions):
print(results)
parse_me(f1 = cp.advparser(),f2 = cp.vikparser())
main()
combinedparser.py具有两个功能:
def advparser(**file):
import pdfplumber
with pdfplumber.open(file) as pdf: # opened fname and assigned it to the variable pdf
page = pdf.pages[0] # assigned index 0 of pages to the variable page
text = page.extract_words()
#followed by a series of python operations generating a dict named Specimen_Output
def vikparser(**file):
with open(file,mode = 'r') as filename:
Specimen_Output = {}
#followed by a series of python operations generating a dict named Specimen_Output
我有一个目录,其中随机散布着pdf和文本文件。我正在尝试使用装饰器@file_discriminator运行功能advparser,该功能使用pdfplumber和后续处理从目录中pdf文件上的pdf文件中提取可用信息;和vikparser对文本文件执行常规的文本文件处理。每个都应生成一个称为Specimen_Output的字典。当advparser是作为advparser(file)运行的单独的.py文件,导入了askopenfilename而不是其复数形式,并使用advparser(file = askopenfilename())调用时,我得到了正确的结果。 vikparser也是如此(它正在查看带有readlines的文本文件)。但是,当我尝试从主模块执行此操作并使用父函数调用它们时,我无法使其正常工作。我已经尝试了几乎所有可能的排列方式,并在“文件”中使用了位置vs关键字参数。
Traceback (most recent call last):
File "<input>",line 1,in <module>
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py",line 197,in runfile
pydev_imports.execfile(filename,global_vars,local_vars) # execute the script
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py",line 18,in execfile
exec(compile(contents+"\n",file,'exec'),glob,loc)
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/main.py",line 29,in <module>
parse_me(f1 = cp.advparser(),f2 = cp.vikparser())
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/combinedparser.py",line 12,in advparser
with pdfplumber.open(file) as pdf: # opened fname and assigned it to the variable pdf
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfplumber/pdf.py",line 48,in open
return cls(path_or_fp,**kwargs)
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfplumber/pdf.py",line 25,in __init__
self.doc = PDFDocument(PDFParser(stream),password=password)
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/pdfparser.py",line 39,in __init__
PsstackParser.__init__(self,fp)
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py",line 502,in __init__
PSBaseParser.__init__(self,line 172,in __init__
self.seek(0)
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py",line 514,in seek
PSBaseParser.seek(self,pos)
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py",line 202,in seek
self.fp.seek(pos)
AttributeError: 'dict' object has no attribute 'seek'
我在做什么错?它在说什么dict对象,为什么当我尝试从askopenfilename()单独调用每种类型时,pdfplumber为什么没有这个问题?我是一个新手编码员,整天都把头发弄乱了。谢谢!
解决方法
问题在于,file
和advparser
函数中的vikparser
自变量实际上是命名自变量的字典,因为它用两个星号定义。所以当您以这种方式调用这些函数
func = f1(file = item)
file
或advparser
函数中的vikparser
参数实际上等于{"file": "some_filename.pdf"}
。
您需要解压缩参数:
def vikparser(**file):
with open(file["file"],mode='r') as filename:
pass
或仅在函数定义中使用单个file
参数:
def vikparser(file):
with open(file,mode='r') as filename:
pass
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。