在我制作的GUI(使用wxpython(中,我需要将文本附加到TextCtrl的特定位置(如果需要,我可以将其更改为其他textEntry(。例如,我有这样的文字:
尤瓦尔是一名冲浪者
他喜欢(这里(去海滩。
我想在单词"点赞"后面加一个单词或几个单词。我如何使用wxpython modoule做到这一点?
如果你总是知道单词,然后想添加其他单词,你可以这样做:
new_text = 'Yuval is a surfer'
search_text = 'likes'
original_text = "He likes to go to the beach."
result = original_text.replace(search_text, " ".join([search_text, new_text]))
print(result)
#Prints: "He likes Yuval is a surfer to go to the beach."
相反,如果你知道的是单词的位置,后面必须添加其他单词:
new_text = 'Yuval is a surfer'
word_pos = 1
original_text = "He likes to go to the beach."
#convert into array:
splitted = original_text.split()
#get the word in the position and add new text:
splitted[word_pos] = " ".join([splitted[word_pos], new_text])
#join the array into a string:
result = " ".join(splitted)
print(result)
#Prints: "He likes Yuval is a surfer to go to the beach."
希望这能有所帮助。