5
$\begingroup$

To achieve the same CSV output as from previous versions Export requires the option "TextDelimiters" -> "". Presumably the default has been changed. Can I change the default back so I don't have to modify code or type in the option every time?

Demonstration code - does not need the option in 11.1

data = {
   {"", "Marmalade", "Rocket Science"},
   {"", "899-01-4862", "32432342324"},
   {"31/12/2017", "", "3428.762324"}};
out = "test.csv";
Export[out, data, "TextDelimiters" -> ""];
in = ReadList[out, Word, WordSeparators -> {","},
   RecordLists -> True, RecordSeparators -> {"\n"},
   NullWords -> True];
data == in

True

Note

One reason for wanting to retain the previous export behaviour is for working with CSVs in both Mathematica and Excel. To explain, opening either CSV outputs below in Excel and saving will result in Output 1 format. So for diffing CSVs and using a predictable input format the Output 1 format is preferable.

enter image description here

A further discovery

When writing a CSV Excel and previous versions of Mathematica put strings with commas in quotes. Use of "TextDelimiters" -> "" knocks out this functionality.

data = {
   {"Q", "IBM, Plc", "Rocket Science"},
   {"", "899-01-4862", "32432342324"},
   {"31/12/2017", "123,456,789.76", "3428.762324"}};
Export["test.csv", data]

enter image description here

$\endgroup$
  • $\begingroup$ I can't find anything for setting the default options for exporting. The only thing I can think is to find the internal function from the System`Convert`* context and use SetOptions on it. It wouldn't stick across kernel sessions though, so you would have to do that in your init.m file $\endgroup$ – Jason B. Jan 11 '18 at 17:45
  • $\begingroup$ this seems relevant, mathematica.stackexchange.com/q/81174/9490, though I can't find anything that works there $\endgroup$ – Jason B. Jan 11 '18 at 18:01
  • $\begingroup$ @JasonB. the function is System`Convert`TableDump`ExportSV. You'll need System`ConvertersDump`LoadExportFormat["CSV"] first. It may have some autoload trigger on it too, because at first I couldn't see its options. $\endgroup$ – b3m2a1 Jan 11 '18 at 19:13
  • $\begingroup$ @b3m2a1 You found the right function. Along with caveats about using internal functions Wolfram support said to evaluate ExportString[{1}, "CSV"];(*can't set the option without autoloading*) SetOptions[SystemConvertTableDumpExportSV, "TextDelimiters" -> ""] ` $\endgroup$ – Chris Degnen Jan 22 '18 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.