5
$\begingroup$

If one wants import data from the first three rows of a file "myfile.data", one can use the command

Import["myfile.data",{"Data",{1,2,3},All}];

However, if I want import the first 100 rows of the file, is there a quick way besides

Import["myfile.data",{"Data",{1,2,3,4,5,....},All}];

And what about the case I need import the last 100 rows of the file?

$\endgroup$
13
$\begingroup$

To import the first 100 rows of the file you can use

Import["myfile.data", {"Data", Range[100]}]

and to import the last 100 rows

Import["myfile.data", {"Data", -Range[100]}]

or

Import["myfile.data", {"Data", Range[-100,-1]}]
$\endgroup$
  • 1
    $\begingroup$ Warning: this reverses the order of rows. For example if your data has 3 rows and you just use Import["my file.dat"] the output will be {row1,row2,row3}. But Import["my file.dat",{"Data",-Range[2]}] will output {row3,row2} $\endgroup$ – Tom Jul 13 '18 at 13:34
5
$\begingroup$

I encourage you to use the following command:

Import["file_name.txt", "Table"][[All, ;;100]]

It works by default with any structured data, while "Data" doesn't. And what's more important - surprisingly it works much faster than importing separated rows or columns - time difference could be the 10-100 times for huge files.

And eventually it's much easier to point what you really want to import with this usual mathematica notation, e.g. take first 5 cols and 100 last rows:

Import["file_name.txt", "Table"][[;;5, -100;;]]
$\endgroup$
  • $\begingroup$ This imports the whole file though, which may become inefficient for large files. The OP is not clear on the requirements... $\endgroup$ – Yves Klett Dec 11 '14 at 14:20
  • $\begingroup$ What means inefficient? and what means large? The point is this command is universal, native for selecting rows/cols and much faster. Say, in case you import something from 10Mb file - it will take 2-4s, while importing as data takes >20s. So for really large files one would wait an hour, instead of a couple of minutes.. Concerning memory consuming - i don't actually know is there really a difference in both commands. Anyway modern computers can easily handle hundreds Mb files, yet files of several gb is surely a different story. $\endgroup$ – funnyp0ny Dec 11 '14 at 14:44
1
$\begingroup$

Use the function 'take'

for example

list = Import["myfile.data",{"Data",{1,2,3},All}];

Take[list1, 100]

you will get the first hundred values

Take[list1, {100,200)]

you wil gett the values hundred until twohundred

$\endgroup$
  • $\begingroup$ Take[list,{100,200}] you mean? $\endgroup$ – Gvxfjørt Sep 11 '17 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.