下面的代碼文字和數字將字符串分割成單詞的列表,但不包括數字:拆分文件名進入在Python
txt="there_once was,a-monkey.called phillip?09.txt"
sep=re.compile(r"[\s\.,-_\?]+")
sep.split(txt)
['there', 'once', 'was', 'a', 'monkey', 'called', 'phillip', 'txt']
此代碼給我文字和數字,但仍包含「_」爲有效字符:
re.findall(r"\w+|\d+",txt)
['there_once', 'was', 'a', 'monkey', 'called', 'phillip', '09', 'txt']
什麼我需要在任何一段代碼來改變與期望的結果結束了:
['there', 'once', 'was', 'a', 'monkey', 'called', 'phillip', '09', 'txt']
如果數字與字母字符相鄰,它們是單詞的一部分還是單獨的單詞?例如,「23skidoo」分爲「[」23「,」skidoo「]還是」[「23skidoo」]? – outis 2010-05-04 01:40:43
應該明確分裂爲[「23」,「skidoo」]。我正在處理文件名,我需要找到月份和日期以確定他們需要分配哪些批次。額外的單詞用於確定文件是否是我們想要的文件,或用於需要用戶干預的錯誤文件。 – danspants 2010-05-04 01:47:33