如何用python编写一个可以拆分多个单词或字符的程序?
例如,我有这些句子:Hi, This is a test. Are you surprised?在这个例子中,我需要我的程序用',','!'来拆分这些句子,'?' 还有“.”。我知道在str库和NLTK中使用split,但我需要知道有没有像split这样的内部pythonic方法?你知道吗
string = 'Hi, This is a test. Are you surprised?'
words = re.split('[,!?.]', string)
print(words)
[u'Hi', u' This is a test', u' Are you surprised', u'']
您正在寻找NLTK包的
tokenize
函数。NLTK
代表自然语言工具包或者从
re
模块中尝试re.split
。你知道吗来自re文件。你知道吗
我想我找到了解决问题的巧妙方法。我不需要使用任何模块。我可以使用str库的
replace
方法,用.
替换像!
或?
这样的词。然后我可以使用split
方法将文本按.
拆分。你知道吗使用重新拆分地址:
相关问题 更多 >
编程相关推荐