views:

291

answers:

2

Hi folks,

I'm looking for a way to read in c++ a text file containing numpy arrays and put the data into vector< vector< ... > > , can anyone help me out please ?

Thanks a lot. Archy

EDIT: format of the text file

[[[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9]] [[10 11] [12 13] [14 15] [16 17] [18 19]] [[20 21] [22 23] [24 25] [26 27] [28 29]] [[30 31] [32 33] [34 35] [36 37] [38 39]]]

Perhaps more readably:

[
    [
        [ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9]
    ]
    [
        [10 11] [12 13] [14 15] [16 17] [18 19]
    ]
    [
        [20 21] [22 23] [24 25] [26 27] [28 29]
    ]
    [
        [30 31] [32 33] [34 35] [36 37] [38 39]
    ]
]
+1  A: 
float val;
::std::vector<float> vals;
ifstream stream("c:/file.txt");
while(stream >> val) {
   vals.push_back(val);
}
Viktor Sehr
I think the real problem here is that `@Archy` as an array of arrays of arrays, so it's a bit more difficult.
Matthieu M.
Thanks anyway for your efforts :)
Archy
Youre right, sry, I missread
Viktor Sehr
Well, in the numpy documentation, they say there is a C-API ... great :)But, I new in C++, can anyone tell me how to include the API inside the C++ code ? (I didn't find any C library to download, so I concluded that it is implicitely installed with Numpy... ?)
Archy
A: 

It's going to depend on your level of expertise.

If you are experienced, I would suggest something like Boost.Spirit.Qi, which is a true parser library. However it might take some time to get used to.

Otherwise it depends on what information you have at your disposal... I'll edit my answer when you provide us with more details since it's hairy enough to potentially get quite complicated :)

Matthieu M.