# Import numeric data into Mathematica

I have a C++ program that outputs a set of numbers at each time step. A blank line separates successive time steps. Here is an example of my data format:

0.2 42.8
0.4 12.3
0.6 32.1
0.8 37.3

0.2 44.2
0.4 17.8
0.6 39.0
0.8 30.1


I want to import this into Mathematica with the following format:

data = {
{{0.2, 42.8}, {0.4, 12.3}, {0.6, 32.1}, {0.8, 37.3}},
{{0.2, 44.2}, {0.4, 17.8}, {0.6, 39.},  {0.8, 30.1}}
};


i.e., the data for each time step is in one list. How do I do this?

• Look at the documentation for ReadList, and particularly the RecordLists->True option setting. Oct 28, 2013 at 4:39
• What excatly is your output? In case of a file, you can also try Import. But for us to help you, you need to provide more details. Oct 28, 2013 at 12:25
• @Verbeia, you should post that as an answer with an example---I didn't know that option and its likely the best approach. Oct 28, 2013 at 14:26
• @george2079 - the question has been closed and as moderator I don't think it's appropriate for me to reopen it just so I can answer it. If you like, I can edit your answer, which I don't think it particularly practical at the moment. Oct 28, 2013 at 20:53
• @Verbeia I hope the post now is clear. And the solution is answered by george2079 below. Thank you all. (this post is on hold because it's unclear but I don't know how to fix it.
– wei
Oct 28, 2013 at 23:49

Assuming you mean you are writing the c++ program .. one approach (of many) to this is to directly write the file in mathematica form, that is simply put all the curly brackets and commas in the file.

You can then simply read it in with Get[].

This may seem a bit ugly, but with this approach you can readily handle cases where you might have variable length data in each time series.

If you just write the raw numbers to the file and use ReadList[] you'll need to tell mathematica how to partition the data.

Edit: approach based on verbia's comment:

Assuming you have a file like this:

1 2
2 4
5 6
<blank>
1 3
5 9
8 3
9 10
<blank>


You can read in the form requested as :

ReadList[filename, {Number, Number},
RecordLists -> True,
RecordSeparators -> {"\n\n"}]


Quite simple and avoids the need to specify the lenght of each record a priori.

• This is exactly what i want thanks!
– wei
Oct 28, 2013 at 23:43

edited with updated file format

DeleteCases[SplitBy[ImportString["0.2 42.8
0.4 12.3
0.6 32.1
0.8 37.3

0.2 44.2
0.4 17.8
0.6 39.0
0.8 30.1", "Table"], # == {} &], {{}}]


{{{0.2, 42.8}, {0.4, 12.3}, {0.6, 32.1}, {0.8, 37.3}}, {{0.2, 44.2}, {0.4, 17.8}, {0.6, 39.}, {0.8, 30.1}}}