Splitting one large file into many if the text in a column doesn't match the text in the one before it

后端 未结 4 998
无人及你
无人及你 2021-01-26 06:31

I searched for awhile and couldn\'t find a response to this. I have a standard tsv file with the following format:

1    100    101    350    A
1    101    102            


        
4条回答
  •  深忆病人
    2021-01-26 06:45

    Here is a small solution in python using groupby and str.rpartition:

    from itertools import groupby
    
    with open("in_file.txt") as f_in:
    for name,lines in groupby(f_in.readlines(),key=lambda x:x.rpartition(" ")[2].strip()):
            with open(f"out_{name}.txt","w") as f_out:
                f_out.writelines(lines)
    

提交回复
热议问题