Re: comma delimited files for input?

• To: mathgroup at smc.vnet.net
• Subject: [mg30798] Re: [mg30774] comma delimited files for input?
• From: Tomas Garza <tgarza01 at prodigy.net.mx>
• Date: Wed, 19 Sep 2001 00:16:37 -0400 (EDT)
• References: <200109100043.UAA00704@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Well, you don't have to go as far as Version 5. The problem is solved in
Version 4.1. But, anyway, if you don't want to upgrade, I suggest you read
in your data from Excel using ReadList["file",Word], which will read your
things in ASCII and you'll have them as strings. You may then transform them
into the proper Mathematica expressions using very simple string operations.
Example: suppose you have your data as 23, 67, 1.2, 0.25 in a file called
"example.csv". Then

In[1]:=
data=ReadList["example.csv",Word][[1]]
Out[1]=
"23,67,1.2,0.25"

places the above string under the name "data". I now give a sequence of
operations that will transform this string into a list of numbers {23, 67,
1.2, 0.25}. This will seem absurdly complicated, but you may put everything
together as a function which you may then simply call whenever needed:

In[2]:=
chars=DeleteCases[Characters[data],"\""]/.","->"*"
Out[2]=
{2,3,*,6,7,*,1,.,2,*,0,.,2,5}

(this separates the characters and changes the original commas into stars,
since commas are cumbersome to work with as characters). Now you find the
positions of the stars in the list of characters:

In[3]:=
pos=Flatten[Position[chars,"*"]]
Out[3]=
{3,6,10}

Now you obtain the starting and ending positions of the sequences of
characters which are separated by stars:

In[4]:=
nots1=Prepend[#+1&/@pos,1]
Out[4]=
{1,4,7,11}
In[5]:=
nots2=Append[#-1&/@pos,Length[chars]]
Out[5]=
{2,5,9,14}
In[6]:=
seqs=Transpose[{nots1,nots2}]
Out[6]=
{{1,2},{4,5},{7,9},{11,14}}

Finally, you take all the separate sequences - previously joined together -
and convert them to numbers (Mathematica expressions) which you can work
with normally:

In[7]:=
ToExpression/@StringTake[StringJoin@@chars,#]&/@p
Out[7]=
{23,67,1.2,0.25}

I assure you this looks worse than it actually is, and works very nicely.

Tomas Garza
Mexico City

----- Original Message -----
From: "1.156" <rob at piovere.com>
To: mathgroup at smc.vnet.net
Subject: [mg30798] [mg30774] comma delimited files for input?

> I'm using version 4.  In order to get plain ascii decimal numbers into my
program I have to use space delimited data files I'm told in the HELP files.
It works fine but I have to make two versions of all the data files because
others need comma delimited
> files of the same data.
>
> It sure would be convenient to be able to use comma delimited files (that
import nicely into Excel) for Mathematica work.  Can anyone tell me whether
there is some guru magic that will allow importing csv files or does it boil
down to upgrading to version 5?
>
> Thanks for any advice on the matter.
>
> Rob
>
>
>
>

```

• Prev by Date: Re: Math 1 Fonts Problem, Another Aspect
• Next by Date: RE: Combinations
• Previous by thread: comma delimited files for input?
• Next by thread: Re: comma delimited files for input?