我有一些代码可以正常使用python中的正则表达式删除标点符号/数字,我不得不更改代码,以便停止列表工作,不是特别重要.无论如何,现在标点符号没有被删除,坦率地说,我很难过为什么.
import re
import nltk
# Quran subset
filename = raw_input('Enter name of file to convert to ARFF with extension, eg. name.txt: ')
# create list of lower case words
word_list = re.split('\s+', file(filename).read().lower())
print 'Words in text:', len(word_list)
# punctuation and numbers to be removed
punctuation = re.compile(r'[-.?!,":;()|0-9]')
for word in word_list:
word = punctuation.sub("", word)
print word_list
Run Code Online (Sandbox Code Playgroud)
关于它为什么不起作用的任何指针都会很棒,我不是python的专家所以它可能是一些非常愚蠢的东西.谢谢.
更改
for word in word_list:
word = punctuation.sub("", word)
Run Code Online (Sandbox Code Playgroud)
至
word_list = [punctuation.sub("", word) for word in word_list]
Run Code Online (Sandbox Code Playgroud)
word在for-loop上面的赋值,只需更改此临时变量引用的值.它没有改变word_list.