bburc
bburc

Reputation: 179

Parse text file between specific lines

So if I have a text file that looks like this, I want to create lists of each block of data.

[Blocktype A]
thing
thing
thing

[Blocktype A]
thing
thing
thing
thing
thing

[Blocktype A]
thing
thing

[Blocktype B]
thing
thing
thing

Essentially I want my code to do this....

If the line == '[Blocktype A]', append the next X number (can vary) of lines to a 'block/stanza' list until the newline is reached. At that point, append this 'block' list to an overall list, empty the 'block' list, and do the same for the next Blocktype A stanza until new line is reached etc...I want to do the same for '[Blocktype B]'.

In the end, I'm trying to get a list that has sub-lists as elements. In other words, a list of [Blocktype A] list data, and a list of all [Blocktype B] list data

bigListA = [ ['Blocktype A', 'thing', 'thing', 'thing'], ['Blocktype A', 'thing', 'thing', 'thing', 'thing', 'thing'], etc...]

bigListB = same as above

I am unsure how to parse between specific lines like this. Any ideas? Thanks so much!

edit* here is my code. the issue with this is, the ['B'] stanzas are getting added to lists they aren't supposed to. I feel like my list emptying steps are off. Another issue I just caught is that when I print out the elements of the returned list, every element is the same (only the first block in the file...it just gets repeated)

def getBlock(myFile):
"""
blah blah blah parses by stanza
"""
print myFile
with open(myFile, 'r') as inFile:
    print '~~~ newfile ~~~\n\n'
    extraData = list()
    blockList = list()
    for line in inFile:
        if line.strip() == '': # skips extraData, start of data blocks
            termBlock = list()
            for line in inFile:
                if line.strip() == '[A]' and len(termBlock) !=0: # A
                    blockList.append(termBlock) # appends termBlock to blockList
                    del termBlock[:] # ensures list is empty for new termBlock
                    termBlock.append(line.strip())
                elif line.strip() == '[B]' and len(termBlock) !=0: # B
                    del termBlock[:]
                    termBlock.append(line.strip())
                elif line.strip() == '': # skip line if it's blank
                    continue
                else: # add all block data
                    termBlock.append(line.strip())
        else:
            metaData.append(line) # adds metaData
    return blockList, metaData

Upvotes: 0

Views: 900

Answers (3)

user5457708
user5457708

Reputation:

The output is exactly what you need

def bigList(list_name,start):
    quit_ask = ""
    list_name = []
    l = []
    check = True
    started = False
    with open("TEXT.txt") as text_file:
        for line in text_file:
            line = line.strip()
            if line.startswith(start) or started == True:
                while '' in l: l.remove('')
                if line.startswith(start):
                    quit_ask = line
                    if check != True:
                        list_name.append(l)
                    l = []
                    l.append(line)
                    started = True
                elif line.startswith('[') and line != quit_ask: break
                else: l.append(line); check = False
    list_name.append(l)
    return list_name

bigListA = []
bigListB = []
bigListA = bigList(bigListA,'[Blocktype A]')
bigListB = bigList(bigListB,'[Blocktype B]')

print bigListA
print bigListB

And you aren't forced to import anything!

Upvotes: 1

rebeling
rebeling

Reputation: 728

Like this:

bigLists = ([z.strip('[').strip(']') for z in y.split('\n') if z]
            for y in x.split('\n\n'))

bigListA = [x for x in bigLists if x[0] == 'Blocktype A']
bigListB = [x for x in bigLists if x[0] == 'Blocktype B']

Upvotes: 1

Robᵩ
Robᵩ

Reputation: 168886

I like to use generator functions for this:

import itertools
from pprint import pprint

def stanzas(f):
    stanza = []
    for line in f:
        line = line.strip()
        if line.startswith('['):
            if stanza:
                yield stanza
            stanza = []
        if line:
            stanza += [line]
    if stanza:
        yield stanza

with open('foo.ini') as input_file:
    all_data = stanzas(input_file)
    all_data = sorted(all_data, key = lambda x:x[0])
    all_data = itertools.groupby(all_data, key = lambda x:x[0])
    all_data = {k:list(v) for k,v in all_data}

# All of the data is in a dict in all_data. The dict keys are whatever
# stanza headers in the file there were.
# We can extract out the bits we want using []
bigListA = all_data['[Blocktype A]']
bigListB = all_data['[Blocktype B]']
pprint(bigListA)
pprint(bigListB)

Upvotes: 3

Related Questions