I'm trying to formulate a parser in python to read an input file and then assemble the results into several arrays.
The data has the 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 the methods that I have tried are:
text_file = 'C:\AA\aa.txt'
lines = open(text_file).read().splitlines()
numbers = []
Vector = []
for 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 I have encountered are the: 1) Having multiple delimiters 2) Trying to split the data according to the relevant sections
I have also tried using the np.genfromtxt along with countless hours trawling the internet.
Your comments and advice are much appreciated.
I am not exactly sure about any in-built or library functions that can do the trick, but there are some apparent issue in your
for
loop .First, a statement after
continue
inside theif
block -numbers.append(line)
(or the vector equivalent) . This statement will never get executed, since continue would send the control back to the starting of thefor
loop , with thecounter
variable incremented.Second, you are not reading based on
sections
, which is what your input contains, though I would say you are not reading anything at all pretty much.A sample code that would work (for numbers and vectors is) -
Please note, the above code is only for numbers and vectors, other sections will need to be coded by you.