Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split file into files with keyword in Python?

I am trying to figure out how to take a file and divide in into sub-files using a keyword as the split indicator. In my case, I have a large file that looks like this:

Racecar
line2...
line3...
Racecar
line5...
line6...
line7...
line8...
Racecar
line10...

At each occurrence of the word Racecar I would like to split the file and make a subfile. Using the above example, File_1 would have 3 lines, File_2 would have 5 lines, and File_3 would have 2 lines. These files would look like this:

File_1:
Racecar
line2...
line3...

File_2:
Racecar
line5...
line6...
line7...
line8...

File_3:
Racecar
line10...

I realize something like sed or awk would be better suited for this, but I need to do this in Python. I'm really stuck on this for some reason. I tried to write something like this:

with open("bigfile", mode="r") as bigfile:
    reader = bigfile.readlines()
    for i,line in enumerate(reader):
        if line.startswith("Racecar"):
            header = line
            header_num = i

I seem to be getting stuck because I can't find a way to get the next occurrence of Racecar. I keep wanting to use the next() function, but obviously this doesn't work on strings. The file I am using is small enough to be read into memory. Can anyone help me with this? Thanks in advance.

like image 845
drbunsen Avatar asked Apr 07 '26 04:04

drbunsen


1 Answers

with open("bigfile", mode="r") as bigfile:
    reader = bigfile.read()
    for i,part in enumerate(reader.split("Racecar")):
        with open("File_" + i+1, mode="w") as newfile:
            newfile.write("Racecar"+part)
like image 156
Vader Avatar answered Apr 09 '26 18:04

Vader