How do I read comma-delimited numbers into Mathematica?
When reading Number format from a file using Read or ReadList, individual numbers are expected to be separated by spaces, tabs, or newline characters, or each number is expected to be in a separate record. If the numbers are separated by other characters, such as commas, you will get an error message indicating that Mathematica is unable to read a number from the file.
In[1]:= !!datafile
10, 20, 30
1.2e1, 3.4e2, 5.6e3
In[2]:= ReadList["datafile", Number]
Read::readn: Syntax error reading a real number from datafile.
Out[2]= {10}
The simplest way to read this type of data is to use the RecordSeparators option to indicate that each number is in a separate record.
In[3]:= data = ReadList["datafile", Number,
RecordSeparators -> {","}]
Out[3]= {10, 20, 30, 12., 340., 5600.}
In this example, since the original file consists of rows of numbers, you may want to use Partition to separate the data into sublists that correspond to the rows in the original file.
In[4]:= Partition[data, 3]
Out[4]= {{10, 20, 30}, {12., 340., 5600.}}
If you for some reason do not want to treat each number as a separate record, there are still a variety of other methods that you can use. The best method will depend on the details of your example.
One method is to read each number as a string, and to convert the strings to numbers in a separate step. Here is a version of this method in which the numbers (as strings) are read using Word format. The strings are then converted to numbers by reading Number format from streams generated using StringToStream.
In[5]:= data = ReadList["datafile", Word,
WordSeparators -> {",", " "},
RecordLists -> True]
Out[5]= {{10, 20, 30}, {1.2e1, 3.4e2, 5.6e3}}
In[6]:= StringToNumber[s_String] :=
Block[{ss, result},
ss = StringToStream[s];
result = Read[ss, Number];
Close[ss];
result
]
In[7]:= Map[StringToNumber, data, {2}]
Out[7]= {{10, 20, 30}, {12., 340., 5600.}}
Another method is to read the separator characters using a format such as Character or Word and to discard the separator characters from the result.
In[8]:= data = ReadList["datafile",
{Number, Character, Number, Character, Number}]
Out[8]= {{10, ,, 20, ,, 30}, {12., ,, 340., ,, 5600.}}
In[9]:= Apply[{#1, #3, #5} &, data, {1}]
Out[9]= {{10, 20, 30}, {12., 340., 5600.}}
There are many variations of these methods, and there are other unrelated methods that are useful in special cases.
If you're using Mathematica 4.0, please see the following FAQ.
http://support.wolfram.com/mathematica/kernel/files/csv4.html
Questions or comments? Send email to support@wolfram.com.
| |