To split a text file into pieces based on the string, how is the operation of splitting the lines?

I have text report files that I need to split (), since the lines are split into arrays.

So the file looks like this:

BOBO: 12341234123412341234
1234123412341234123412341
123412341234
BOBO: 12349087609812340-98
43690871234509875
45

BOBO: 32498714235908713248
0987235

And I want to create 3 subfiles from this line break starting with "^ BOBO:". I really don't want 3 physical files, I would prefer 3 different file pointers.

+3
source share
2 answers

Maybe use itertools.groupby :

import itertools

def bobo(x):    
    if x.startswith('BOBO:'):
        bobo.count+=1
    return bobo.count
bobo.count=0

with open('a') as f:
    for key,grp in itertools.groupby(f,bobo):
        print(key,list(grp))

gives:

(1, ['BOBO:12341234123412341234\n', '1234123412341234123412341\n', '123412341234\n'])
(2, ['BOBO:12349087609812340-98\n', '43690871234509875\n', '45\n', '\n'])
(3, ['BOBO:32498714235908713248\n', '0987235\n'])

, , . cStringIO:

import cStringIO
with open('a') as f:
    file_handles=[]
    for key,grp in itertools.groupby(f,bobo):
        file_handles.append(cStringIO.StringIO(''.join(grp)))

file_handles , "BOBO:".

+3

, , , , - :

subFileBlocks = []

with open('myReportFile.txt') as fh:
  for line in fh:
    if line.startswith('BOBO'):
      subFileBlocks.append(line)
    else:
      subFileBlocks[-1] += line

subFileBlocks .

+1

Source: https://habr.com/ru/post/1763167/


All Articles