AttributeError:运行Tika解析器时,“bytes”对象没有属性“close” - python

我试图使用Tika运行简单的代码解析行来解析PDF中的文本(在此示例中命名为outputFileName)。这曾经运行没有错误。我最近将我的笔记本电脑发送到我们的IT部门进行软件更新,并且不得不树脂化Anaconda并导入某些模块(例如2019年11月9日发布的Tika 1.22)来运行我的代码。我还从Tika导入了解析器。这是代码行和我收到的错误消息:

码:

#从pdf文件中解析文本以使用

pdftext = parser.from_file(outputFileName)

错误

    AttributeError                            Traceback (most recent call last)
    <ipython-input-14-239522b5ef31> in <module>
          1 #parsing the text out of pdf file to work with
    ----> 2 pdftext = parser.from_file(outputFileName)

    ~\AppData\Local\Continuum\anaconda3\lib\site-packages\tika\parser.py in from_file(filename, serverEndpoint, xmlContent, headers, config_path, requestOptions)

         34     '''
         35     if not xmlContent:
    ---> 36         jsonOutput = parse1('all', filename, serverEndpoint, headers=headers,config_path=config_path, requestOptions=requestOptions)

         37     else:
         38         jsonOutput = parse1('all', filename, serverEndpoint, services={'meta': '/meta', 'text': '/tika', 'all': '/rmeta/xml'},

    ~\AppData\Local\Continuum\anaconda3\lib\site-packages\tika\tika.py in parse1(option, urlOrPath, serverEndpoint, verbose, tikaServerJar, responseMimeType, services, rawResponse, headers, config_path, requestOptions)
        327     headers.update({'Accept': responseMimeType, 'Content-Disposition': make_content_disposition_header(path)})
        328     status, response = callServer('put', serverEndpoint, service, open(path, 'rb'),
    --> 329                                   headers, verbose, tikaServerJar, config_path=config_path, rawResponse=rawResponse, requestOptions=requestOptions)
        330 
        331     if file_type == 'remote': os.unlink(path)

    ~\AppData\Local\Continuum\anaconda3\lib\site-packages\tika\tika.py in callServer(verb, serverEndpoint, service, data, headers, verbose, tikaServerJar, httpVerbs, classpath, rawResponse, config_path, requestOptions)
        544 
        545     resp = verbFn(serviceUrl, encodedData, **effectiveRequestOptions)
    --> 546     encodedData.close() # closes the file reading data
        547 
        548     if verbose:

    AttributeError: 'bytes' object has no attribute 'close'

我还下载了Java 8。

tika.py源代码中有问题吗?还是我错过了需要安装的东西?任何帮助将非常感激。

参考方案

只需安装旧版本的tika。我遇到了同样的问题,它对我有用。怎么样?转到终端并输入pip install tika==1.19

AttributeError:'AnonymousUserMixin'对象没有属性'can' - python

烧瓶学习问题为了定制对匿名用户的要求,我在模型中设置了一个类: class MyAnonymousUser(AnonymousUserMixin): def can(self, permissions): return False def is_administrator(self): return False login_manager.anonymous…

Qt5:AttributeError:'模块'对象没有属性'QApplication' - python

系统:15.10(Wily Werewolf)x64码:# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'test.ui' # # Created by: PyQt5 UI code generator 5.5.1 # # WARNI…

AttributeError:'NoneType'对象没有属性'traverse' - python

我目前正在构建Flask应用程序,并且正在尝试在其中加载Plotly Dash应用程序。我能够从下面的stackoverflow留言板上得到这些信息,但是我认为我做的事情不正确(可能忽略了某些事情?)Running a Dash app within a Flask app这里的任何帮助将不胜感激这是我的server.py代码:from flask impo…

单行的'if'/'for'语句是否使用Python样式好? - python

我经常在这里看到某人的代码,看起来像是“单线”,这是一条单行语句,以传统的“if”语句或“for”循环的标准方式执行。我在Google周围搜索,无法真正找到可以执行的搜索类型?任何人都可以提出建议并最好举一些例子吗?例如,我可以一行执行此操作吗?example = "example" if "exam" in exam…

为什么使用'=='或'is'比较字符串有时会产生不同的结果? - python

我有一个Python程序,其中将两个变量设置为'public'值。在条件表达式中,我有比较var1 is var2失败,但如果将其更改为var1 == var2,它将返回True。现在,如果我打开Python解释器并进行相同的“是”比较,则此操作成功。>>> s1 = 'public' >>…