我有一个输入文本文件:
This file contains information of students who are joining picnic:
@@@ bob alice rhea john
mary alex roma
peter &##&现在,我想在查找@@标记时,在列表中添加学生姓名&在查找&##&标记时停止追加。输出应该是(使用python):
bob alice rhea john mary alex roma peter 发布于 2021-05-25 04:48:32
您可以使用此方法
txt = """This file contains information of students who are joining picnic:
@@@ bob alice rhea john
mary alex roma
peter &##&"""或:
txt = open("./file.txt").read()
ls = txt.split("@@@")[1].split("&##&")[0].split()
print(ls)这些指纹:
['bob', 'alice', 'rhea', 'john', 'mary', 'alex', 'roma', 'peter']发布于 2021-05-25 04:42:57
re.findall与re.sub的结合
inp = """This file contains information of students who are joining picnic:
@@@ bob alice rhea john
mary alex roma
peter &##&"""
output = re.sub(r'\s+', ' ', re.findall(r'@@@\s+(.*?)\s+&##&', inp, flags=re.DOTALL)[0])
print(output) # bob alice rhea john mary alex roma peter如果您想要一个列表,请使用:
output = re.split(r'\s+', re.findall(r'@@@\s+(.*?)\s+&##&', inp, flags=re.DOTALL)[0])
print(output)这些指纹:
['bob', 'alice', 'rhea', 'john', 'mary', 'alex', 'roma', 'peter']https://stackoverflow.com/questions/67681831
复制相似问题