Message Boards Message Boards

Import data from a text file?

GROUPS:

Hello everyone,

I have data files with the following form : "name of block 1", then 3 columns of data, then two blank lines, then second block and repeat. I attached an example of such a file to this post.

I would like to open this file in a notebook, and generate lists with it. The name of the lists should be the name of the blocks (one list per block), and their content should be : { {X1, Y1}, {X2, Y2}, ...} such that Xi and Yi are the values in the two first columns of the blocks.

I know that should be feasible with the Import command, but I do not know exactly how...

Does someone has an idea for achieve that ? :)

Thank you very much

Attachments:
POSTED BY: Quentin Marolleau
Answer
5 days ago

What have you tried? I don't see your code. Have a look at the documentation: CSV and Import.

POSTED BY: Sander Huisman
Answer
5 days ago

Usually I am using this very simple method :

data = Import["/mypath"]

This command returns a list with the elements of the file, and it works fine when I am working with files with just columns of data, without blocks or titles.

But here it does not work, because the software should detect each block of data thanks to the two line breaks between them... (I do not know the length of each block). I do not want to create a huge list, mixing up all the data.

POSTED BY: Quentin Marolleau
Answer
5 days ago

> should detect each block of data

That is not part of the CSV specification I believe, it can only handle 2D data, not 3D as you want. However you can split the data in to blocks right?

fn = "exemple.csv";
data = Import[fn, "TSV"];
data = Split[data, # =!= {""} &];    (* split at empty row *)
data = DeleteCases[data, {{""}} | {""}, \[Infinity]]   (* remove empty records *)

Now you have to replace -inf probably with something that is understood, similarly for nan...

POSTED BY: Sander Huisman
Answer
5 days ago

Ok thank you for this model. I will need some time to understand your commands, and then we will see if it inspires me ^^

thanks again

POSTED BY: Quentin Marolleau
Answer
5 days ago

Group Abstract Group Abstract