TypeError:stat:路径应该是字符串、字节、os.PathLike 或整数,而不是 _io.TextIOWrapper

问题描述 投票:0回答:2

我在一个python教程网站上找到了以下代码:

from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize


stanford_classifier = open(r"C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\classifiers\english.all.3class.distsim.crf.ser.gz")
stanford_ner_path = open(r"C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\stanford-ner.jar")


st = StanfordNERTagger(stanford_classifier, stanford_ner_path)

text = 'While in France, Christine Lagarde discussed short-term stimulus efforts in a recent interview with the Wall Street Journal.'

tokenized_text = word_tokenize(text)
classified_text = st.tag(tokenized_text)

print (classified_text)

错误如下:

Traceback (most recent call last):
  File "C:/Users/DELL7810/AppData/Local/Programs/Python/Python37/stanpar.py", line 9, in <module>
st = StanfordNERTagger(stanford_classifier, stanford_ner_path)
 File "C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\lib\site-packages\nltk\tag\stanford.py", line 180, in __init__
super(StanfordNERTagger, self).__init__(*args, **kwargs)
File "C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\lib\site-packages\nltk\tag\stanford.py", line 63, in __init__
verbose=verbose)
File "C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\lib\site-packages\nltk\internals.py", line 721, in find_jar
searchpath, url, verbose, is_regex))
File "C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\lib\site-packages\nltk\internals.py", line 632, in find_jar_iter
if os.path.isfile(path_to_jar):
File "C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\lib\genericpath.py", line 30, in isfile
st = os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not _io.TextIOWrapper
python python-3.x stanford-nlp
2个回答
1
投票

你的问题

正如您在此文档页面中所看到的,

StanfordNERTagger
将文件路径作为参数:

StanfordNERTagger(path_to_model, path_to_jar)

您的代码崩溃,因为

open()
将为您提供文件对象,而这不是
StanfordNERTagger
所期望的参数。

解决方案

直接将路径作为参数提供给

StanfordNERTagger
,如下所示:

st = StanfordNERTagger("C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\classifiers\english.all.3class.distsim.crf.ser.gz", "C:\Users\DELL7810\AppData\Local\Programs\Python\Python37\stanford-ner-2018-02-27\stanford-ner.jar")

0
投票

当我尝试使用仅需要文件路径的库转换通过 Streamlit 前端上传的文件时,遇到了标题中的错误。由于 Streamlit 的

file_uploader()
不跟踪最近上传的文件的路径,所以这是一个问题。无论如何,为了解决这个问题,我创建了一个临时文件,将其路径传递给文件读取器,然后将其删除。

示例代码(在 Streamlit 和 langchain 上)如下所示:

import streamlit as st
from langchain.document_loaders import PyPDFLoader

uploaded_file = st.file_uploader("Upload a file:", type=['pdf'])
loader = PyPDFLoader(uploaded_file)            # <---- TypeError: stat: path should be string, bytes, os.PathLike or integer, not UploadedFile

解决这个问题

import os
from tempfile import NamedTemporaryFile

bytes_data = uploaded_file.read()
with NamedTemporaryFile(delete=False) as tmp:  # open a named temporary file
    tmp.write(bytes_data)                      # write data from the uploaded file into it
    data = PyPDFLoader(tmp.name).load()        # <---- now it works!
os.remove(tmp.name)                            # remove temp file
© www.soinside.com 2019 - 2024. All rights reserved.