1
$\begingroup$

I have some problems with importing a .txt file as a table. This is the file. I tried importing it with

data = Import["data.txt", "Data"] 

I want to access single values, but it doesn't work with my import. I can't access data[[2, 1]] for example to get the first value in data[[2]] and so on. Any ideas?

$\endgroup$
0

2 Answers 2

5
$\begingroup$
xxx = Import["C:\\Users\\Rasher\\Downloads\\data.txt", "Data"];
zzz = xxx // StringSplit;
zzz[[2, 2]]

Gives 774...

You need to decide how / where to "split" or do other operations, since this is basically a bulk text file, e.g., split at line-breaks, etc. Read the documentation on StringSplit and ancillary text manipulation functions, once you've imported it these will let you massage it into the desired MMA list/array structure.

With just the above, row 1 of zzz is what appear to be the column headings, so zzz[[1,n]] gets you the n'th one. Row 2 to ... are the rows, with the first entry the row name, e.g, zzz[[2,1]] gets the name of the first row under headings, zzz[[2,2]] the first row entry, etc.

$\endgroup$
1
  • $\begingroup$ Ahh StringSplit, of course. Works fine now..thanks :) $\endgroup$
    – holistic
    Feb 27, 2014 at 11:59
1
$\begingroup$

Here is another way that does it for you in one go:

data = ReadList["https://dl.dropboxusercontent.com/u/19921198/data.txt", Word,
               RecordLists -> True];

So that:

data[[2, 2]]

gives 774

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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