ReadList
["file"]ReadList
["file", type]ReadList
["file", {$type_1$, $type_2$, ...}]To read all the numbers in a file and return a list of them:
ReadList["ExampleData/numbers.txt", Number]
(Use FilePrint[]
to get the raw data for the examples above and below.)
This does the same, but groups the numbers in to a pairs:
ReadList["ExampleData/numbers.txt", {Number, Number}]
Now let us read and put blocks of 3 numbers in its own list:
ReadList["ExampleData/numbers.txt", Table[Number, {3}]]
Like Read[]
, ReadList
handles types of objects other than numbers.
We can read a list of characters in a file putting each character as an item in a list:
ReadList["ExampleData/strings.txt", Character]
And now, here are the integer codes corresponding to each of the bytes in the file:
ReadList["ExampleData/strings.txt", Byte]
But the data can also be read by “words”:
ReadList["ExampleData/strings.txt", Word]
The above uses the default value which is space of some sort., However you can set your own value:
ReadList["ExampleData/strings.txt", Word, WordSeparators -> {"e", "."}]
See WordSeparators for more information.
Reading by records uses the separators found in
ReadList["ExampleData/strings.txt", Record]
See RecordSeparators works analgously for records as WordSeparators
does for words.
To allow both periods and newlines as record separators:
ReadList["ExampleData/sentences.txt", Record, RecordSeparators -> {".", "\n"}]
See also Reading Textual Data.