3 回答

TA貢獻1827條經驗 獲得超8個贊
您可以使用re.split...
from string import punctuation
import re
puncrx = re.compile(r'[{}\s]'.format(re.escape(punctuation)))
print filter(None, puncrx.split(your_tweet))
或者,只查找包含某些連續字符的單詞:
print re.findall(re.findall('[\w#@]+', s), your_tweet)
例如:
print re.findall(r'[\w@#]+', 'talking about #python with @someone is so much fun! Is there a 140 char limit? So not cool!')
# ['talking', 'about', '#python', 'with', '@someone', 'is', 'so', 'much', 'fun', 'Is', 'there', 'a', '140', 'char', 'limit', 'So', 'not', 'cool']
我最初在示例中確實有一個笑臉,但是當然這些最終都被這種方法過濾掉了,因此需要警惕。

TA貢獻1851條經驗 獲得超5個贊
我建議使用以下代碼從特殊符號中清除文本:
tweet_object["text"] = re.sub(u'[!?@#$.,#:\u2026]', '', tweet_object["text"])
您需要先導入re,然后再使用function sub
import re
添加回答
舉報