python - Reading input data to array(s) with multiple delimiters and headings -
i'm trying formulate parser in python read input file , assemble results several arrays.
the data has following structure:
some_numbers 1 5 6 some_vector [-0.99612937 -0.08789929 0. ] [-0.99612937 -0.08789929 0. ] [ -9.99999987e-01 1.61260621e-04 0.00000000e+00] some_data 1239 #int 671 471 851 s4rs #string 517 18 48 912 s4rs
so far methods have tried are:
text_file = 'c:\aa\aa.txt' lines = open(text_file).read().splitlines() numbers = [] vector = [] line in lines: line = line.strip() if line.startswith('some_numbers'): continue numbers.append(line) if line.startswith('some_vector'): continue vector.append(line)
problems have encountered the: 1) having multiple delimiters 2) trying split data according relevant sections
i have tried using np.genfromtxt along countless hours trawling internet.
your comments , advice appreciated.
i not sure in-built or library functions can trick, there apparent issue in for
loop .
first, statement after continue
inside if
block - numbers.append(line)
(or vector equivalent) . statement never executed, since continue send control starting of for
loop , counter
variable incremented.
second, not reading based on sections
, input contains, though not reading @ pretty much.
a sample code work (for numbers , vectors is) -
text_file = 'c:\aa\aa.txt' lines = open(text_file).read().splitlines() numbers = [] vector = [] section = '' line in lines: line = line.strip() if line.startswith('some_numbers'): section = 'numbers' continue elif line.startswith('some_vector'): section = 'vectors' continue elif section == 'numbers': numbers.append(line) # or numbers.append(int(line)) , whichever want elif section == 'vectors': vector.append(line)
please note, above code numbers , vectors, other sections need coded you.
Comments
Post a Comment