如何提取字符串前后组中的数据子集
How to extract subset of data in groups before and after a string
我有一个文本文件。在基于特定单词的文本文件中,它应该将数据分为两组,例如特定单词之前的所有内容作为一组,特定单词之后的所有内容作为另一组
像这样的文本文件
hello every one
Is any space here?
CHAIN
every thing of the
file lies here
基于CHAIN我们将文本文件分成两组
group 1
hello every one
Is any space here?
group 2
every thing of the
file lies here
您提到您有一个文本文件 test.txt
。
您的代码:
with open("test.txt", "r") as f:
data = f.readlines()
part1, part2 = ("".join(data).split("CHAIN"))
print(part1)
print(part2)
给我:
hello every one
Is any space here?
every thing of the
file lies here
否则其他方案也不错
您可以尝试拆分解决方案,并使用下面给出的索引访问每个字符串。
a = """
hello every one
Is any space here?
CHAIN
every thing of the
file lies here
"""
print(a.split("CHAIN")[0])
print(a.split("CHAIN")[1])
只是为了完整性(其他答案也有效):
如果您有文本文件
file = open('file.txt', 'r').read()
print(file.split('CHAIN'))
# if you want to remove the new spaces (\n)
print([text.strip() for text in file.split('CHAIN')])
我有一个文本文件。在基于特定单词的文本文件中,它应该将数据分为两组,例如特定单词之前的所有内容作为一组,特定单词之后的所有内容作为另一组
像这样的文本文件
hello every one
Is any space here?
CHAIN
every thing of the
file lies here
基于CHAIN我们将文本文件分成两组
group 1
hello every one
Is any space here?
group 2
every thing of the
file lies here
您提到您有一个文本文件 test.txt
。
您的代码:
with open("test.txt", "r") as f:
data = f.readlines()
part1, part2 = ("".join(data).split("CHAIN"))
print(part1)
print(part2)
给我:
hello every one
Is any space here?
every thing of the
file lies here
否则其他方案也不错
您可以尝试拆分解决方案,并使用下面给出的索引访问每个字符串。
a = """
hello every one
Is any space here?
CHAIN
every thing of the
file lies here
"""
print(a.split("CHAIN")[0])
print(a.split("CHAIN")[1])
只是为了完整性(其他答案也有效):
如果您有文本文件
file = open('file.txt', 'r').read()
print(file.split('CHAIN'))
# if you want to remove the new spaces (\n)
print([text.strip() for text in file.split('CHAIN')])