I have a text file that has below general form:

name of file
some additional information
value1 data data data data
value2 data data data data
value2 data data data data
value2 data data data data
value2 data data data data
value2 data data data data

I want to import the file and extract just "data" from it. Namely in this example I need to select the rows 4-6 and 8-10 and then columns 2-5. How can I write such code? This code gives me just one set of specific rows,

Import["name.txt", {"Data", Range[4, 6]}]

but I don't know how to specify columns. Note that all information are separated by one space. Thanks in Advance.

  • $\begingroup$ Try Part[ Import[ "name.txt", "Table"] , {4, 5, 6, 8, 9, 10}, 2 ;; 5] $\endgroup$ – Jason B. Mar 26 '18 at 16:08
  • $\begingroup$ You'll also want to look up the HeaderLines option to strip out the headings at the beginning. $\endgroup$ – J. M. will be back soon Mar 26 '18 at 16:10
  • $\begingroup$ @JasonB. Thanks a lot. I try this Jason Part[Import["name.txt", "Table"], Range[14, 29], 2 ;; 6] for my original file and it worked. is there a general command for specifying rows and columns of a imported file in Mathematica? $\endgroup$ – Wisdom Mar 27 '18 at 5:53
  • $\begingroup$ @ J. M. needs help. Thanks $\endgroup$ – Wisdom Mar 27 '18 at 6:40
  • $\begingroup$ @NSR in general you were on the right track.. Import["name.txt", {"Table", "Data", Join[Range[4, 6], Range[8,10]], Range[2, 5]}] works.. The general form for import of 2d data is {format, element, rows, columns}, so to just take columns it would be {"Table", "Data", All, Range[2,5]}. For "Table" only Ranges will work, but for CSV and XLSX spans will work as well in 11.3.. We're working on updating these even more, and hope Table will be updated also in the future. $\endgroup$ – GenericAccountName Jun 28 '18 at 15:01

Your Answer

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

Browse other questions tagged or ask your own question.