Search code examples
pythonreadlinestrip

How to readline to find a match string and split the result into multiple files


This is the scenario, I've a pattern file that I needed to read line by line.

The content of the pattern file somewhat similar to this

chicken 
chicken
chicken
chicken
## comment
## comment
fish
fish
chicken
chicken
chicken

The code I've came up so far is like this.

def readlines_write():
    with open(filename) as rl:
        for line in rl:
            if "chicken" in line:
                with open(new_filename, 'a+') as new_rl:
                    new_rl.write(line)

With above code I can find all "chicken" in that pattern file and the result would be write in new_filename. But that is not the objective. Because I've lump sum it all together in one file.

I would like to split the chicken and write it to multiple files.

Eg. In the end result should be, read line by line continuously, if found chicken and stop when the next line is not contain chicken. Break and write it to a file, a.out for example.

And the script continue read line by line and found the next matches after the "comment" and "fish". And write the result to b.out

I've the pseudo in mind but I'm not sure how to translate it to python logic.

Summary, I would like to split the chicken that got separated by the comment and other words than chicken.


Solution

  • Just add an else condition and keep changing the name of the file by an integer or timestamp.

    def readlines_write():
            i = 0
            new_filename = 'filename{}.out'.format(i)
            with open(filename) as rl:
                for line in rl:
                    if "chicken" in line:
                        with open(new_filename, 'a+') as new_rl:
                            new_rl.write(line)
                    else:
                        i +=1
                        new_filename = 'filename{}.out'.format(i)