First ever code review post, so I hope I'm doing it right. My understanding of this site is that it is a place to post stuff like this for feedback and criticism. If that isn't the case, I apologise for my misunderstanding.
I'm teaching myself Python 3 and I'm pretty near the start. I couldn't find a simple description of a file parser, so I spent a frankly embarrassing amount of time trying to craft one, and this is the result:
def parse(file, startTag, endTag): bears=[] bearFile = open(file, 'r') for bearLine in bearFile: if startTag in bearLine: bear = bearFile.readline() while not (endTag in bear): bears.append(bear.strip()) bear = bearFile.readline() return bears
I tested it using:
print(parse("bears.txt", "==start==", "==end=="))
...on a file which looked like this:
This file contains a list of bears. Bears are below, between the start and end tags. ==start== Grizzly Polar Koala Panda Spectacled Sun ==end== These are some more tags, listing some false bears: ==start== Purple Hairless Aquatic Flying ==end==
...and it works! The output is:
['Grizzly', 'Polar', 'Koala', 'Panda', 'Spectacled', 'Sun', 'Purple', 'Hairless', 'Aquatic', 'Flying']
With some obvious flaws, including:
1 - I can't work out how to ignore empty lines between the tags without using another if. I wondered if it might be better to do this later, and strip out all the '' entries, perhaps as an argument option.
2 - It depends on new lines because of the readline
method, so using something else like a comma as a delimiter is currently beyond me.
3 - If there is no end tag, it loops forever. try
and except
are quite new to me right now.
I hope I'm not just wasting everyone's time by being here...
bearFile
.\$\endgroup\$