Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1349

Re: read CSV

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: read CSV
Date 2011-03-31 09:06 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <in1g5r$4q5$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 3/30/11 at 4:13 AM, alan.isaac@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.

Back to comp.soft-sys.math.mathematica | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: read CSV Bill Rowe <readnews@sbcglobal.net> - 2011-03-31 09:06 +0000
  Re: read CSV Alan <alan.isaac@gmail.com> - 2011-04-14 08:59 +0000

csiph-web