我从报纸上抓下了标题,我也从头文中删除了停用词,但在删除了停用词之后,这个单词带有单引号,所以我不想要这些引用,为此我尝试下面的代码:
from nltk.corpus import stopwords
blog_posts=[]
stop = stopwords.words('english')+[
'.',
',',
'--',
'\'s',
'?',
')',
'(',
':',
'\'',
'\'re',
'"',
'-',
'}',
'{',
u'—',
'a', 'able', 'about', 'above', 'according', 'accordingly', 'across', 'actually', 'after', 'afterwards', 'again', 'against', 'all', 'allow', 'allows', 'almost', 'alone', 'along', 'already', 'also', 'although', 'always', 'am', 'among', 'amongst', 'an', 'and', 'another', 'any', 'anybody',
]
file=open("resources/ch05-webpages/newspapers/TOI2232014.csv","r+")
t=[i for i in file.read().split() if i not in stop]
blog_posts.append((t,))
print blog_posts
所以这段代码的输出是:
[(['"\'Duplicates\'', 'BJP,', 'Jaswant', 'Singh', 'says"', '"Flight'],)]
但我想要这样的输出:
[([Duplicates,BJP,Jaswant,Singh,ays,Flight])]
那么我能为这个输出做些什么呢?
答案 0 :(得分:2)
t=[i.replace("\\","").replace("\"","").replace("\'",").strip()
for i in file.read().split() if i not in stop]
如果您知道要删除的详尽字符列表,那么将是一种草率的方式。
如果你知道你只想要字母字符
import re
t=[re.findall([a-aA-Z]+, i) for i in file.read().split()
if i not in stop]
答案 1 :(得分:0)
t=[i.replace("\'","").replace("?","").replace(":","").replace("\"","").replace("#","").strip()
for i in file.read().split() if i not in stop]
#blog_posts.append((t,))
p=' '.join(t)
blog_posts.append((p,))
print blog_posts