【问题标题】:Split a string variable into lines based on space between characters [closed]根据字符之间的空格将字符串变量拆分为行[关闭]
【发布时间】:2021-01-08 16:16:12
【问题描述】:

我有一个字符串变量(文本),如下所示:

text = 'Be specific and      imagine you’re   asking a question    to another         person'

字符串的单词之间存在随机空格(就字符长度而言)。我想以这样的方式将字符串变量拆分成行,当 2 个大于 3 个字符(或空格)的单词之间有空格时,字符串的其余部分会被拆分并粘贴到下面的 2 行。另外,我希望被分割的字符串部分从行首开始(并且在行首没有空格字符)。 所以最终结果应该是:

'Be specific and      

imagine you’re   

asking a question    

to another         

person'

有什么建议吗?

【问题讨论】:

标签: python


【解决方案1】:
import re
text = 'Be specific and      imagine you’re   asking a question    to another         person'

for item in re.split(r" {2,}", text):
    print(item)

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多