views:

519

answers:

4

I have a CSV file that is formatted like:

0.0023709,8.5752e-007,4.847e-008

and I would like to import it into Mathematica and then have each column separated into a list so I can do some math on the selected column.

I know I can import the data with:

Import["data.csv"]

then I can separate the columns with this:

StringSplit[data[[1, 1]], ","]

which gives:

{"0.0023709", "8.5752e-007", "4.847e-008"}

The problem now is that I don't know how to get the data into individual lists and also Mathematica does not accept scientific notation in the form 8.5e-007.

Any help in how to break the data into columns and format the scientific notation would be great.

Thanks in advance.

+6  A: 

KennyTM is correct.

data = Import["data.csv", "CSV"];
column1 = data[[All,1]]
column2 = data[[All,2]]
...
Davorak
Here is the documentation for `Import[..., "CSV"]`:http://reference.wolfram.com/mathematica/ref/format/CSV.html
Michael Pilat
+2  A: 

You can fix the notation by using StringReplace[].

In[1]: aa = {"0.0023709", "8.5752e-007", "4.847e-008"};

In[2]: ToExpression[
          StringReplace[
             #,
             RegularExpression@"(^\d+\.\d+)e([+-]\d+)" -> "$1*10^$2"
          ]
       ] & @ aa

Out[2]: {0.0023709, 8.5752*10^-7, 4.847*10^-8}

You can put the entire data array in place of aa to process is all at once with a one liner

{col1,col2,col3} = ToExpression[...] & @ Transpose[Import["data.csv", "CSV"]];

with ToExpression[...] as above.

Timo
+2  A: 

Davorak's answer is the correct one if you need to import a whole CSV file as an array. However, if you have a single string that you need to convert from the C/Fortran-style exponential notation, you can use ImportString with different arguments for the format. As an example, there's

In[1]:= ImportString["1.0e6", "List"]
Out[1]= {1.*^6}

The *^ operator is Mathematica's equivalent of the e. Note this is also a good way to split apart strings that are in CSV form:

In[2]:= ImportString["1.0e6,3.2,foo", "CSV"] 
Out[2]= {{1.*10^6,3.2,foo}}

In both cases, you'll get your answer wrapped up in an extra level of list structure, which is pretty easy to deal with. However, if you're really sure you only have or want a single number, you can turn the string into a stream and use Read. It's cumbersome enough that I'd stick to ImportString, however:

In[3]:= Module[{stream = StringToStream["1.0e6"], number},
          number = Read[stream, "Number"];
          Close[stream];
          number]
Out[3]= 1.*10^6
Pillsy
A: 

In MMA7, I use the "elements" argument. In fact, I can't Import even a .csv file without specifying the element:

aa=Import["data.csv","Data"]

When you do this, all strings are automatically converted to expressions: Head/@Flatten@aa is {Real, Real, ....}. Also, "8.5752e-007" becomes 8.5752*10^7, a legal MMA expression.

The result of the Import is a 1xn list {{ ... }}.

So, Transpose@aa gives the nx1 list {{.},{.}, .... }.

I think this is the format you wanted.

Mark E Harder