Group Abstract Group Abstract

Message Boards Message Boards

0
|
12.6K Views
|
3 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Export strings without quotes to a .csv?

Posted 7 years ago

Writing a file from Mathematica for use by another program. The output file will be a comma separated file. Content of the file will be a combination of numbers and text. The text in the output file should not have quotes around it. Below is a sample data set and what I have tried so far.

row0 = {"h1", "h2", "h3", "h4", "h5", "h6"};
row1 = {"", "", "a1", 1.01, "", ""};
row2 = {"", "", "b1", 2.02, "", ""};
row3 = {"", "", "c1", 3.03, "", ""};
data = {row0, row1, row2, row3};
Export["data.csv", data];
Export["data.txt", data];
Export["data.dat", data];
Export["data.tsv", data];
Export["data.xls", data];

A summary of the results:

  • data.csv -- has quotes around the strings
  • data.txt -- has quotes around the strings and curly braces around each line
  • data.dat -- no quotes around the strings, but the data is separated by tabs
  • data.tsv -- has quotes around the strings
  • data.xls -- no quotes around strings, data in appropriate columns, but this is an Excel file, which can't be read by the other program.

One successful work-around:: save the files as data.xls, then from Excel export to a .csv

Is there a way to Export the data so that it does not have quotes around the strings, but is separated by commas?

(Follow-up question: how to include quotes in some of the exported text. The Excel workaround fails for this case.)

POSTED BY: Robert McHugh
3 Replies

Set the option "TextDelimiters":

Export["data.csv",{{"a\nb", "c", "d"}}, "CSV", "TextDelimiters" -> ""]
POSTED BY: Sander Huisman
Posted 7 years ago

Thanks Neil,

That does address the first question very nicely!

As for the follow-up question, in the example below, the output has two set of double quotes like this: text with""quoted text"" . The goal would be to have only one set of double quotes, like this: text with "quoted text"

row0 = {"h1", "h2", "h3", "h4", "h5", "h6"};
row1 = {"", "", "a1", 1.01, "", ""};
row2 = {"", "", "b1", 2.02, "", "text with \"quoted text\" "};
row3 = {"", "", "c1", 3.03, "", ""};
data = {row0, row1, row2, row3};
Export["data.csv", data, "TextDelimiters" -> ""]; 

Output is the following

h1,h2,h3,h4,h5,h6
,,a1,1.01,,
,,b1,2.02,,text with ""quoted text"" 
,,c1,3.03,, 

, just need to change the "" to "

POSTED BY: Robert McHugh
POSTED BY: Neil Singer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard