我有一个文本文件。在基于特定单词的文本文件中,应将数据分为两组,例如特定单词之前的所有内容都作为1组,特定单词之后的所有内容均作为另一组
文本文件是这样的
hello every one
Is any space here?
CHAIN
every thing of the
file lies here
基于链,我们将文本文件分为两组
group 1
hello every one
Is any space here?
group 2
every thing of the
file lies here
答案 0 :(得分:0)
您提到您有一个名为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
否则其他解决方案也很好。
答案 1 :(得分:0)
您可以尝试使用以下所示的索引对每个字符串进行分割ans的解决方案。
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])
答案 2 :(得分:0)
仅出于完整性考虑(其他答案也适用)
如果您有文本文件
/opt/freeware/lib/python2.7/lib-dynload/binascii.so
/opt/freeware/lib64/python2.7/lib-dynload/binascii.so