MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: read CSV

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117802] Re: read CSV
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 31 Mar 2011 04:06:07 -0500 (EST)

On 3/30/11 at 4:13 AM, alan.isaac at gmail.com (Alan) wrote:

>I've received some files formatted like below. How can I read
>selected columns into a list? I'm coming from a background where I'd
>read in each line, discarding the first, then (in this case at
>least) split on the comma, and coerce the type.  So I'm happy to do
>that if I can figure out how.  (Naturally I could preprocess the
>data into a form that say ReadList likes better, but I'm looking for
>a pure Mathematica solution.)

>Thanks, Alan Isaac

>"x","y","color","pen down?"
>"0","0.26161459854014585","105","true"
>"1","0.2665251497682828","105","true"
>"2","0.29244385081680907","105","true"
>"3","0.31995416628471895","105","true"

If I were to do this entirely within Mathematica, I would read
the data in by doing

data=Import[filename, "CSV"];

Then select the columns I wanted by doing something like

data[[All,{1,4}]]

This would select the 1st and 4th columns.

Finally, I assume you want the numbers as numbers not strings.
So, I would convert the strings to numbers using ToExpression.

But frankly, I regard this as rather cumbersome. Since I use a
Mac and have an excellent text editor with a much better grep
engine than what is available in Mathematica, I would
pre-process the file using that tool into a form acceptable to
Get and use Get to read the file. In fact, I find it so handy to
use my text editor with Mathematica, I have set up a Mathematica
function that accepts a filename as an argument and has the file
opened up by my text editor. Additionally, I have grep macros
set up in the text editor to do the type of pre-processing needed.

Mathematica is a very powerful tool that can be used for a great
many purposes. But it is not the most efficient tool to use for
text processing.



  • Prev by Date: Re: read CSV
  • Next by Date: Re: Off Topic: E-Mail Security in this Group
  • Previous by thread: Re: read CSV
  • Next by thread: Re: read CSV