0

所以这是我的代码:

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 没有这个问题?我是一个新手编码器,整天都在扯头发。谢谢!

4

1 回答 1

-1

问题是你的file参数advparservikparser函数实际上是一个命名参数的字典,因为它是用两个星号定义的。所以当你以这种方式调用这些函数时

func = f1(file = item)

fileadvparserorvikparser函数中的参数实际上等于{"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
于 2020-09-22T07:50:39.450 回答