Python 2.7 从nltk中删除停止字后如何删除引号?
我从报纸上截取了标题,也从标题中删除了stopwords,但在删除stopwords后,该词带有单引号,因此我不想要这些引号,为此我尝试了以下代码:Python 2.7 从nltk中删除停止字后如何删除引号?,python-2.7,nltk,stop-words,Python 2.7,Nltk,Stop Words,我从报纸上截取了标题,也从标题中删除了stopwords,但在删除stopwords后,该词带有单引号,因此我不想要这些引号,为此我尝试了以下代码: from nltk.corpus import stopwords blog_posts=[] stop = stopwords.words('english')+[ '.', ',', '--', '\'s', '?', ')', '(', ':', '\'', '\
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])]
那么我能为这个输出做些什么呢
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]
我终于得到了这个问题的答案
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
它工作得很好,但它没有删除单引号,它只删除了单引号内的(/,“,/”),例如:'prashant's'->'prashant',但我想->Prashantyu使用了我的答案的一个非常轻微的修改版本,发布了它,然后接受了它?