Skip to content Skip to sidebar Skip to footer

Reading A File For A Certain Section In Python

I am trying to follow a answer given here: How to only read lines in a text file after a certain string using python? in reading only the line after a certain phrase in which I wen

Solution 1:

You have to skip the rest of the for loop after detecting the "header". In your code, you're setting found_type to True and then the if found_type: check matches.

found_type = False
t_ype = [] 
with open('test.xml', 'r') as f:
    for line in f:
        if '<type>' in line:
            found_type = True
            continue                    # This is the only change to your code.
                                        # When the header is found, immediately go to the next line
        if found_type:
            if '</type>' in line:
               found_type = False               
            else:    
                t_line = str(line).rstrip('\n')
                t_ype.append(t_line)

Solution 2:

The simplest approach is a double loop with yield:

def section(fle, begin, end):
    with open(fle) as f:
        for line in f:
            # found start of section so start iterating from next line
            if line.startswith(begin):
                for line in f: 
                    # found end so end function
                    if line.startswith(end):
                        return
                    # yield every line in the section
                    yield line.rstrip()     

Then just either call list(section('test.xml','<type>','</type>')) or iterate over for line in section('test.xml','<type>','</type>'):use lines,if you have repeating sections then swap the return for a break. You also don't need to call str on the lines as they are already strings, if you have a large file then the groupby approach in the comments might be a better alternative.


Post a Comment for "Reading A File For A Certain Section In Python"