首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:'file‘对象没有'lower’属性

错误:'file‘对象没有'lower’属性
EN

Stack Overflow用户
提问于 2016-11-25 05:45:39
回答 1查看 950关注 0票数 0

我正在使用Stopword过滤器,我给脚本提供了一个包含文章的文件的路径。然而,我得到了错误:

代码语言:javascript
复制
Traceback (most recent call last):
File "stop2.py", line 17, in <module>
print preprocess(sentence)
File "stop2.py", line 10, in preprocess
sentence = sentence.lower()
AttributeError: 'file' object has no attribute 'lower'

关于如何将文件作为参数传递的想法,我的代码也附在下面

代码语言:javascript
复制
# -*- coding: utf-8 -*-
from __future__ import division, unicode_literals
import string
import nltk
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
import re

def preprocess(sentence):
    sentence = sentence.lower()
    tokenizer = RegexpTokenizer(r'\w')
    tokens = tokenizer.tokenize(sentence)
    filtered_words = [w for w in tokens if not w in stopwords.words('english')]
    return " ".join(filtered_words)

sentence = open('pathtofile')
print preprocess(sentence)
EN

Stack Overflow用户

发布于 2016-11-25 05:49:53

sentence = open(...)表示语句是file的一个实例(从open()方法返回);

而您似乎希望拥有文件的全部内容:sentence = open(...).read()

票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40794878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档