Tuesday, 20 August 2013

How to read data like this from a text file?

How to read data like this from a text file?

The text file is like
101 # an integer
abcd # a string
2 # a number that indicates how many 3-line structures will there be below
1.4 # some float number
2 # a number indicating how many numbers will there be in the next line
1 5 # 2 numbers
2.7 # another float number
3 # another number
4 2 7 # three numbers
and the output should be like [101,'abcd',[1.4,[1,5]],[2.7,[4,2,7]]]
I can do it line by line, with readlines(), strip(), int(), and for loop,
but I'm not sure how to do it like a pro.
P.S. there can be spaces and tabs and maybe empty lines randomly inserted
in the text file. The input was originally intended for C program where it
doesn't matter :(

No comments:

Post a Comment