简体   繁体   English

如何在字符串之前和之后提取组中的数据子集

[英]How to extract subset of data in groups before and after a string

I have a text file. 我有一个文本文件。 In text file based on specific word it should make the data into two groups like everything before specific word as 1 group and every thing after specific word as another group 在基于特定单词的文本文件中,它应该将数据分成两组,例如特定单词之前的所有单词作为1组,并且将特定单词之后的所有内容作为另一组

text file some thing like this 文本文件有点像这样

hello every one 
Is any space here?

CHAIN

every thing of the 

file lies here

Based on CHAIN we separate text file into two groups 基于CHAIN,我们将文本文件分成两组

group 1
hello every one 
Is any space here?
group 2
every thing of the 

file lies here

You mentioned you have a text file say test.txt . 你提到你有一个文本文件说test.txt

You code: 你编码:

with open("test.txt", "r") as f:
    data = f.readlines()

part1, part2 = ("".join(data).split("CHAIN"))
print(part1)
print(part2)

Gives me: 给我:

hello every one
Is any space here?




every thing of the

file lies here

Otherwise other solution is also good. 否则其他解决方案也很好。

you can try a solution with split ans access each string using index as given below. 您可以尝试使用split 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])

just for completeness (other answers work as well): 只是为了完整性(其他答案也有效):

if you have a text file 如果你有一个文本文件

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')])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Python 中每个正则表达式匹配组之前和之后添加字符串? - How to add string before and after each regex matched groups in Python? 从字符前后的数据列(字符串)中提取元素 - Extract elements from data column (String) before and after character 如何通过定义分隔符前后来提取子字符串 - How to extract sub string by defining before and after delimiter 将对象的子集提取为字符串 - Extract a subset of an object as string 如何在使用 Python 下载之前对气候数据进行子集化? - How to subset climate data before downloading with Python? Python在字符前后搜索/提取字符串 - Python search / extract string before and after a character 如何根据 pandas 列中最后一次出现的字符串提取 dataframe 的子集? - How to extract a subset of dataframe based on the last occurence of a string in a column in pandas? 根据 python 中的正则表达式匹配提取字符串之前和之后的字符串 - Extract string before and string after based on a regex match in python 如何只删除字符串前面而不是后面的数字? (在数据框内) - How to only remove the numbers that are before a string and not after? (inside data frame) 如何使用 for 循环在我的模式之前和之后提取特定长度的字符串 - How can I use for loop to extract a specific length of string before and after my pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM