【问题标题】: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']