我有一個字符串a
,我想返回一個列表b
,其中包含的單詞不是從@
或#
開始,也不包含任何非單詞字符。Python如何將字符串拆分爲包含單引號的單詞?
但是,我很難將「他們」這樣的單詞保留爲單個單詞。請注意,「Okay .... so」這樣的詞應該分成兩個單詞「好吧」和「如此」。
我覺得問題可以通過修改正則表達式來解決。謝謝!
a = "@luke5sos are you awake now?!!! me #hashtag time! [email protected] over, now okay....so they're rich....and hopefully available?"
a = a.split()
b = []
for word in a:
if word != "" and word[0] != "@" and word[0] != "#":
for item in re.split(r'\W+\'\W|\W+', word):
if item != "":
b.append(item)
else:
continue
else:
continue
print b
什麼是從這個預期的結果? – hwnd 2014-10-06 03:23:00
['是','你','醒來','現在','我','時間','是','超過','現在','好','是',「他們是「,'rich','and','hopefully','available'] – 2014-10-06 03:24:31