7

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?

Karsten7
  • 27,448
  • 5
  • 73
  • 134
  • Related (about large files): http://mathematica.stackexchange.com/questions/14656/dealing-with-large-files and http://mathematica.stackexchange.com/questions/36/file-backed-lists-variables-for-handling-large-data/209#209 – Yves Klett Dec 11 '14 at 15:01

3 Answers3

13

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]}]
Karsten7
  • 27,448
  • 5
  • 73
  • 134
  • 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
6

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;;]]
funnyp0ny
  • 1,905
  • 14
  • 21
  • 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
2

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

Michiel van Mens
  • 2,835
  • 12
  • 23