简体   繁体   中英

Python read series of information in text file between two strings

I have a text file with the following type of format:

BEGIN *A information here* END
BEGIN *B information here* END
BEGIN *C information here*
    *C additional information here*
    *C additional information here*
    BEGIN *C secondary information here*
          *C additional secondary information*
          BEGIN *C tertiary information* END
    END
    BEGIN *C secondary information*
    END
END
BEGIN *D information here* END

I want to read the information between BEGIN and END and keep the information in the same sort of format, as a list of lists. I have tried replacing 'BEGIN' and 'END' with '[' and ']' respectively, and then tried to evaluate the resulting string, but it throws a syntax error when it hits a number in the information. This is the code I tried:

with open(filepath) as infile:
mylist = []
for line in infile:
    line = line.strip()
    line = line.replace('BEGIN', '[')
    line = line.replace('END', ']')
    mylist.append(line)

for n in mylist:
    print n

which produces:

[ *A information here* ]
[ *B information here* ]
[ *C information here*
*C additional information here*
*C additional information here*
[ *C secondary information here*
*C additional secondary information*
[ *C tertiary information* ]
]
[ *C secondary information*
]
]
[ *D information here* ]

Is there any way to get the data out as a list of lists like so:

>>>for n in mylist:
>>>   print n
[*A information here*]
[*B information here*]
[*C information here* *C additional information here* [*C secondary information here* *C additional secondary information* [*C tertiary information*]] [*C secondary information*]]
[*D information here*]

Assuming the file doesn't contain any brackets, you could replace "BEGIN" and "END" with brackets like you did, then write a recursive function to parse it:

def parse(text):
    j=0
    result = [""]  # initialize a list to store the result
    for i in range(len(text)):  # iterate over indices of characters
        if text[i] == "[":
            s = ""  # initialize a string to store the text
            nestlevel = 1  # initialize a variable to store number of nested blocks
            j = i
            while nestlevel != 0:  # loop until outside all nested blocks
                j+=1
                # increment or decrement nest level on encountering brackets
                if text[j]=="[":
                    nestlevel+=1
                if text[j]=="]":
                    nestlevel-=1
            # data block goes from index i+1 to index j-1
            result.append(parse(text[i+1:j]))  # slicing doesn't include end bound element
            result.append("")
        elif i>j:
            result[-1]=result[-1]+text[i]
    return result
with open(filepath) as f:
    data=parse(f.read().replace("BEGIN","[").replace("END","]"))

This is just a rough idea, and I'm sure it could be optimized and improved in other ways. Also, it might return empty strings where there was no text between sub-lists.

I have managed to get it working with the following code:

def getObjectData(filepath):
    with open(filepath) as infile:
        mylist = []
        linenum = 0
        varcount = 0
        varlinedic = {}
        for line in infile:
            line = line.replace('BEGIN', '[').replace('END', ']')
            linenum += 1
            if line.startswith('['):
                varcount += 1

            varlinedic[varcount] = linenum
            mylist.append(line.strip())

    for key in varlinedic:
        if key == varlinedic[key]:
            print mylist[varlinedic[key]-1:varlinedic[key]]
        else:
            print mylist[varlinedic[key-1]:varlinedic[key]]

print getObjectData(filepath)

It returns:

['[ *A information here* ]']
['[ *B information here* ]']
['[ *C information here*', '*C additional information here*', '*C additional information here*', '[ *C secondary information here*', '*C additional secondary information*', '[ *C tertiary information* ]', ']', '[ *C secondary information*', ']', ']']
['[ *D information here* ]']
None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM