拆分字符串每个第 N 个字符并用不同的分隔符将其连接回去

Split string each Nth character and join it back with different separators

我尝试在带有不同分隔符的句子中使用文字换行。这正是我想要得到的输出:

'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() 函数可以更好地完成我想做的事情,但我找不到方法。请问,您知道如何实现吗?

Wrap 为您提供可迭代的文本。如果您可以使用分隔符创建可迭代对象,则可以使用 "".join(t for pair in zip(wrapped_chunks, separators) for t in pair)

加入它们

您可以使用无限生成器创建分隔符:

def inf_separators():
    index = 1
    while True:
        yield f"SEPARATOR{index}"
        index = index + 1

这会给你一个分隔符太多,所以你可能想删除它或特别附加 wrapped_chunks 的最后一项。

如果你想在几个不同的分隔符之间交替,你可以使用 itertools.cycle(["SEP1", "SEP2", "SEP3"]) 来生成一个重复的标记循环。

试试这个:

from textwrap import wrap

sentence = '''here are third-party extensions like Scener that allow us to watch content.'''

new_sentence = ""
parts = wrap(sentence, 20)
for i, part in enumerate(parts):
    new_sentence += part
    # adding separator after each part except for the last one
    if i < len(parts) - 1:
        new_sentence += f"[SEPARATOR{i+1}]"
print(new_sentence)

# output: here are third-party[SEPARATOR1]extensions like[SEPARATOR2]Scener that allow us[SEPARATOR3]to watch content.

这是您可以尝试的一种衬垫:

text = ''.join([(f'[SEPARATOR{i}]' if i else '') + w
                for i, w in enumerate(wrap(sentence, 20))])