# Import data from first/last 100 rows of a file

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?

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]}]

• 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} – Tom Jul 13 '18 at 13:34

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;;]]

• This imports the whole file though, which may become inefficient for large files. The OP is not clear on the requirements... – Yves Klett Dec 11 '14 at 14:20
• 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. – funnyp0ny Dec 11 '14 at 14:44

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

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