【发布时间】:2020-05-29 13:06:09
【问题描述】:
我尝试在具有不同分隔符的句子中使用文本换行。这正是我想要得到的输出:
'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() 函数可能是一种更好的方法来做我想做的事,但我找不到如何做。请问,您知道如何实现这一目标吗?
【问题讨论】:
标签: python string delimiter word-wrap