Created
January 19, 2017 18:21
-
-
Save ael-code/5a0362347dc034b4fec3cc3931bba281 to your computer and use it in GitHub Desktop.
stupid parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
NUM_REGEXP = "[0-9]+\.?[0-9]*" | |
def parse_data(input_file): | |
with open(input_file, 'r') as fb: | |
num = parse_single_number(fb) | |
matrix = parse_matrix(fb, cast=float) | |
fb.readline() | |
loc_min = parse_vector(fb) | |
fb.readline() | |
loc_max = parse_vector(fb) | |
fb.readline() | |
loc_duration = parse_vector(fb) | |
return (num, matrix, loc_min, loc_max, loc_duration) | |
def parse_single_number(fb, cast=int): | |
return parse_vector(fb, cast)[0] | |
def parse_vector(fb, cast=int): | |
v = re.findall(NUM_REGEXP, fb.readline()) | |
return list(map(cast, v)) | |
def parse_matrix(fb, cast=int): | |
matrix = [] | |
v = [] | |
while len(v) == 0: | |
v = parse_vector(fb, cast) | |
while len(v) != 0: | |
matrix.append(v) | |
v = parse_vector(fb, cast) | |
return matrix |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment