Message Boards Message Boards

0
|
3510 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Import data from a text file?

Posted 7 years ago

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:
4 Replies

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

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.

> 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

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

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract