所以这是我的代码:
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())
主要的()
其中 combineparser.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 是一个单独的 .py 文件作为 advparser(file) 运行时,我得到了正确的结果,导入 askopenfilename 而不是它的复数,并使用 advparser(file = askopenfilename()); vikparser 也是如此(它正在查看带有 readlines 的文本文件)。但是,当我尝试从主模块执行此操作并使用父函数调用它们时,我无法让它工作。我'
当我修复因改变事物而产生的任何错误时,这是我得到的最常见的错误:
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, fp)
File "/Users/zachthomasadmin/PycharmProjects/pythonProject1/venv/lib/python3.8/site-packages/pdfminer/psparser.py", 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 没有这个问题?我是一个新手编码器,整天都在扯头发。谢谢!