3

I'm trying to load multiple vectors and matrices (for numpy) that are stored in a single text file. The file looks like this:

%VectorA
1 2 3 4
%MatrixA
1 2 3
4 5 6
%VectorB
3 4 5 6 7

The ideal solution would be to have a dictionary object like:

{'VectorB': [3, 4, 5, 6, 7], 'VectorA': [1, 2, 3, 4], 'MatrixA':[[1, 2, 3],[4, 5, 6]]}

The order of the variables can be assumed as fixed. So, a list of the numpy arrays in the order of appearance in the text file would also be okay.

4
  • Can you still change the format in your text files or is this something you have to live with? Commented Apr 17, 2013 at 10:48
  • it flexible as long as one knows which data is where. but the values should be space separated. Commented Apr 17, 2013 at 10:51
  • 1
    Why do you need to keep the values space separated? I mean are there some special reasons why you couldn't use numpy.load and numpy.save? Commented Apr 17, 2013 at 10:53
  • 1
    I actually meant "white space" separated. :) Because I do copy&paste these from the console output of an existing c++ application, and I cant modify the the vector and matrix output. Otherwise I would probably have used the pickle module if it was from python to python. Commented Apr 17, 2013 at 13:21

1 Answer 1

5
from StringIO import StringIO
mytext='''%VectorA
1 2 3 4
%MatrixA
1 2 3
4 5 6
%VectorB
3 4 5 6 7'''

myfile=StringIO(mytext)
mydict={}
for x in myfile.readlines():
    if x.startswith('%'):
        mydict.setdefault(x.strip('%').strip(),[])
        lastkey=x.strip('%').strip()
    else:
        mydict[lastkey].append([int(x1) for x1 in x.split(' ')])

above gives mydict as:

{'MatrixA': [[1, 2, 3], [4, 5, 6]],
 'VectorA': [[1, 2, 3, 4]],
 'VectorB': [[3, 4, 5, 6, 7]]}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.