【问题标题】:How to convert text to list in python如何将文本转换为python中的列表
【发布时间】:2022-11-27 12:00:12
【问题描述】:

请在此处添加一些上下文。 问题不见了。

input="""
intro: hey,how are you
i am fine

intro:
hey, how are you
Hope you are fine
"""

output= [['hey,how are you i am fine'],['hey, how are you Hope you are fine']]

for text in f:
    text = text.strip()

【问题讨论】:

  • 请澄清你的问题。

标签: python list file text logic


【解决方案1】:

我会研究正则表达式之类的东西或者只是使用

input.split("intro:") 生成字符串列表。这不会导致您在下面的表格。但是由于您的问题不清楚,那是我能做的最好的。

【讨论】:

    【解决方案2】:

    我们可以在这里以多行模式使用re.findall

    inp = """intro: hey,how are you i am fine
    
    intro: hey, how are you Hope you are fine"""
    
    lines = re.findall(r'^w+:s*(.*)$', inp, flags=re.M)
    print(lines)
    

    这打印:

    ['hey,how are you i am fine', 'hey, how are you Hope you are fine']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 2019-08-14
      • 1970-01-01
      • 2016-07-11
      • 1970-01-01
      相关资源
      最近更新 更多