You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EDGAR-reports-Text-Analysis作业报错TypeError:预期路径类对象而非DataFrame

问题根因

报错核心原因是你传入open()函数的stopWordsFile变量实际是Pandas DataFrame类型,而open()仅支持字符串格式的文件路径、字节路径或os.PathLike类型的对象作为入参,类型不匹配触发报错。

解决方案

根据你的实际使用场景二选一即可:

场景1:需要读取本地停用词文本文件

先确认停用词文件的本地存储路径,将路径字符串赋值给stopWordsFile后再执行读取逻辑,同时修复原代码换行符的语法问题,修改后代码如下:

# 替换为你自己的停用词文件实际路径
stopWordsFile = "./stopwords.txt"
with open(stopWordsFile ,'r', encoding='utf-8') as stop_words:
    stopWords = stop_words.read().lower()
stopWordList = stopWords.split('\n')
# 过滤空字符串和首尾空白字符,兼容不同换行格式的文件
stopWordList = [word.strip() for word in stopWordList if word.strip()]

场景2:停用词已经存储在stopWordsFile这个DataFrame中

如果之前已经把停用词读取到了DataFrame里,不需要再调用open()读文件,直接从DataFrame中提取停用词即可,示例代码如下:

# 假设停用词存储在DataFrame的第一列,可替换为你实际的列名,比如stopWordsFile["停用词列名"]
stopWordList = stopWordsFile.iloc[:,0].str.lower().tolist()
# 过滤空值和空白字符
stopWordList = [word.strip() for word in stopWordList if isinstance(word, str) and word.strip()]

额外注意:原代码中拆分换行符的写法存在语法问题,应该使用转义字符\n表示换行,不要直接把换行符写在引号内部,避免不同操作系统下出现编码、解析异常。

内容的提问来源于stack exchange,提问作者Pranav Jaswal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 03:15:08