python将首字符移到末尾_Python:将符号移到字符串的开头?

我正在尝试创建一个崇高的文本插件(使用python),它可以反转所选字符串中单词的顺序。我已经有了主要的功能,但是现在我的问题是单词末尾的每个符号(句点、逗号、问号等等)都保持在原来的位置,我的目标是让所有的符号都正确地颠倒过来,这样符号就应该移到单词的开头。在def run(self, edit):

selections = self.view.sel()

# Loop through multiple text selections

for location in selections:

# Grab selection

sentence = self.view.substr(location)

# Break the string into an array of words

words = sentence.split()

# The greasy fix

for individual in words:

if individual.endswith('.'):

words[words.index(individual)] = "."+individual[:-1]

# Join the array items together in reverse order

sentence_rev = " ".join(reversed(words))

# Replace the current string with the new reversed string

self.view.replace(edit, location, sentence_rev)

# The quick brown fox, jumped over the lazy dog.

# .dog lazy the over jumped ,fox brown quick The

我已经能够遍历每个单词并使用endswith()方法进行快速修复,但这不会找到多个符号(没有一个长长的if语句列表),也无法解释多个符号并将它们全部移动。在

我一直在玩正则表达式,但仍然没有一个有效的解决方案,我一直在寻找一种方法来更改符号的索引,但仍然没有任何结果。。。在

如果我能提供更多的细节,请告诉我。在

谢谢!在


版权声明:本文为weixin_32869031原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。