I'm parsing a file and am using if
statements to "find" sections in the file. The logic done in the if
statement takes in the data from that section holds and parses it. At the end of each if
statement the current line is set to the end of that section. These files are around 200,000 lines and the script is processing x number of files at a time; I'm trying to keep efficiency in mind.
Here's a code sample:
section = ["section1", "section2", "section3"]
with open(file, 'r') as f:
for line in f:
if section[0]:
parseFileSection()
line = current_line
if section[1]:
parseFileSection()
line = current_line
if section[2]:
parseFileSection()
line = current_line
Certain sections are only seen once in the file. Is there a way to eliminate an if
statement from the for loop so that it's not checked? Is the time used to check x if
statements negligible?
One thought is to somehow loop through the section
array and pop that value off when it's "used". But this will involve a lot of repeated checking as well. In addition to that, some sections are not parsed the same way, so it's not as simple as checking the section, calling a parse function, and popping off that section.
Aucun commentaire:
Enregistrer un commentaire