Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mathematica 6.0 CSV export with ";" as delimiter?

833 views
Skip to first unread message

kirk reinholtz

unread,
Aug 30, 2007, 11:57:38 PM8/30/07
to
I find I need to Export a file from Mathematica 6.01 in CSV format,
except I need to use semicolon for the delimiter, rather than comma.

The documentation alludes to "many options" that might influence how
this might be accomplished, but I can't find the options themselves.

So please, granting I may be blind, where is this documented and how
is it done??

Thanks in advance...


Jean-Marc Gulliet

unread,
Sep 1, 2007, 12:35:30 AM9/1/07
to
What you are really looking for is exporting a table data format (file
extension .dat by default) with the *FieldSeparators* option set to
semicolon in your case.

In[1]:= data = RandomReal[{-1, 1}, {4, 5}]

Out[1]= {{0.868671, -0.583131, -0.205539,
0.451934, -0.179933}, {-0.892268, 0.853666, -0.936663,
0.909701, -0.910825}, {-0.0781347, 0.339566,
0.490868, -0.126004, -0.169031}, {0.944671,
0.400847, -0.63365, -0.115168, -0.448351}}

In[2]:= Export["myfile.dat", data, "FieldSeparators" -> ";"]

Out[2]= "myfile.dat"

(* and if you really insist in keeping the extension csv *)

In[3]:= Export["myfile.csv", data, "Table", "FieldSeparators" -> ";"]

Out[3]= "myfile.csv"

--
Jean-Marc


kirk reinholtz

unread,
Sep 1, 2007, 12:36:31 AM9/1/07
to
Asked and answered!

It appears that in 6.0.1 Export["outfile",dat,"CSV"] is implemented
as (or at least behaves like) Export
["outfile",dat,"table",FieldSeparators->","].

So, I can get any delimiter by changing the fieldSeparators
option...plus the documentation on the table format is much more
extensive than the documentation on the csv format.

Thanks in advance for your help, plus any other helpful inputs I
receive...

On Aug 30, 2007, at 8:48 PM, kirk reinholtz wrote:

> I find I need to Export a file from Mathematica 6.01 in CSV format,
> except I need to use semicolon for the delimiter, rather than comma.
>
> The documentation alludes to "many options" that might influence how
> this might be accomplished, but I can't find the options themselves.
>
> So please, granting I may be blind, where is this documented and how
> is it done??
>

> Thanks in advance...
>
>


Jean-Marc Gulliet

unread,
Sep 1, 2007, 12:37:32 AM9/1/07
to
[Answer at the bottom of the post]

kirk reinholtz wrote:

> When I import this, how do I cause each field to be interpreted as a
> String, even if it looks like a number???
>
> Thanks again.
>
> On Aug 31, 2007, at 7:12 AM, Jean-Marc Gulliet wrote:

> > What you are really looking for is exporting a table data format
> > (file extension .dat by default) with the *FieldSeparators* option
> > set to semicolon in your case.
> >
> > In[1]:= data = RandomReal[{-1, 1}, {4, 5}]
> >
> > Out[1]= {{0.868671, -0.583131, -0.205539,
> > 0.451934, -0.179933}, {-0.892268, 0.853666, -0.936663,
> > 0.909701, -0.910825}, {-0.0781347, 0.339566,
> > 0.490868, -0.126004, -0.169031}, {0.944671,
> > 0.400847, -0.63365, -0.115168, -0.448351}}
> >
> > In[2]:= Export["myfile.dat", data, "FieldSeparators" -> ";"]
> >
> > Out[2]= "myfile.dat"
> >
> > (* and if you really insist in keeping the extension csv *)
> >
> > In[3]:= Export["myfile.csv", data, "Table", "FieldSeparators" -> ";"]
> >
> > Out[3]= "myfile.csv"

Mathematica has a built-in conversion function called *ToString* that
converts an expression into a string of characters. (The reciprocal is
*ToExpression*). Since you have a list of lists, you could use *Map*
with its third argument set to {2} (or {-1}), to apply *ToExpression*
to numbers (or atomic expressions) only. For instance,

Map[ToString, Import["myfile.dat", "FieldSeparators" -> ";"], {-1}]

--
Jean-Marc

Bill Rowe

unread,
Sep 1, 2007, 12:51:43 AM9/1/07
to
On 8/30/07 at 11:48 PM, kirk.re...@jpl.nasa.gov (kirk reinholtz)
wrote:

>I find I need to Export a file from Mathematica 6.01 in CSV format,
>except I need to use semicolon for the delimiter, rather than comma.

>The documentation alludes to "many options" that might influence how
>this might be accomplished, but I can't find the options themselves.

You can find details of the options as follows:

from the Documentation Center page for Export
click the arrow for More Information
then click link "Listing of All Formats"

This will bring up the guide for importing and exporting. In
this guide, clicking on one of the formats will bring up a page
for that format.

In particular, entering ref/format/CSV as the search term in the
Documentation Center will take you directly to the page for the
CSV format.

But, none of the available options will do what you are requesting

I think the easiest way to achieve what you want would be to
export as "CSV" then use a text editor to repacle all of the
commas with semicolons.

If I wanted to do this entirely within Mathematica, I would try
something like

Export[filename,
StringReplace[ExportString[#,"CSV"],{"{"->"","}"->"",","->";"]&/@data,
"List"]

where data contains the stuff you want exported.
--
To reply via email subtract one hundred and four

0 new messages