从文本块中切出最后一个单词的最佳方式是什么?Python:切断句子的最后一个单词?
我能想到它
- 斯普利特的列表(按空格),并删除最后一个项目,然后reconcatenating列表。
- 使用正则表达式替换最后一个单词。
我目前录取办法#1,但我不知道如何连接列表...
content = content[position-1:position+249] # Content
words = string.split(content, ' ')
words = words[len[words] -1] # Cut of the last word
任何代码示例大加赞赏。
与一些其他的答案,如果觉得有必要要注意rsplit是反向分割(不是正则表达式分割),1是maxsplit。 – 2016-06-30 16:36:31
@ duckman_1991好点 - 延长了答案。 – 2018-01-16 17:14:57