我尝试在带有不同分隔符的句子中使用文本换行。这正是我想要得到的输出结果:
'here are third-party[SEPARATOR1]extensions like[SEPARATOR2]Scener that allow us[SEPARATOR3]to watch content.'
这是我第一次尝试使用.join()
和wrap()
,但失败了:
[In] :
sentence = '''here are third-party extensions like Scener that allow us to watch content.'''
separator = '[SEPARATOR]'
text = separator.join(wrap(sentence, 20))
[Out] :
'here are third-party[SEPARATOR]extensions like[SEPARATOR]Scener that allow us[SEPARATOR]to watch content.'
然后,我尝试在分隔符内部使用for循环,但也没有成功...:
[In] :
sentence = '''here are third-party extensions like Scener that allow us to watch content.'''
for i in range(1, 4):
separator = '[SEPARATOR' + str(i) + ']'
text = separator.join(wrap(sentence, 20))
[Out] :
'here are third-party[SEPARATOR3]extensions like[SEPARATOR3]Scener that allow us[SEPARATOR3]to watch content.'
也许将.split()
和.join()
函数组合起来使用可以更好地实现我想要的效果,但我不知道如何做。请问你有关于如何实现这一点的任何想法吗?
enumerate
,对吧?再次感谢。 - prog-amateur