7
$\begingroup$

I'm trying to export the following string

"sep = ,"

to a CSV-file, as to be able to define the separation symbol.

However, when using the following command:

Export["test.csv", Join[{"sep = ,"}, RandomInteger[{-5, 5}, {5, 3}]], "CSV"]

I get the quotes around the string in the first line:

"sep = ,"
-4,5,0
5,-4,-1
0,5,2
-1,2,4
-1,-4,-1

This obviously isn't recognized by Excel and it doesn't open correctly. I've tried changing the string to something like

Style["sep=,", ShowStringCharacters -> False]

and then export again, but then I just get this whole command into the CSV-file.

Is there a method to export this string without the quotes attached to them into a CSV-file?

$\endgroup$
3
  • $\begingroup$ AFAIK, strings can only be exported without the quotation marks if done via streams, as in stream=OpenWrite[file];WriteString[stream, "string..."];Close@stream or if OpenWrite is called with the option FormatType -> OutputForm, but that might cause unwanted formatting on other characters. $\endgroup$ Jan 24, 2014 at 18:19
  • 2
    $\begingroup$ Of curiosity is there some software that actually needs that header? Undoubtedly it will break many things (eg Mathematica will just include it as a data line if you import..) $\endgroup$
    – george2079
    Jan 24, 2014 at 18:52
  • $\begingroup$ Actually, this is standard behavior for Excel exports to CSV too. If a string contains a field separator (a comma in this case) it needs to be quoted otherwise it will be seen (incorrectly) as a field separator upon import. Excel will automatically quote the string $\endgroup$ Apr 27, 2014 at 19:54

2 Answers 2

13
$\begingroup$
 Export["test.csv", Join[{"sep = ,"}, 
        RandomInteger[{-5, 5}, {5, 3}]], "TextDelimiters"->None]

(With the obvious issue if you also have strings that should be quoted in the data )

$\endgroup$
5
$\begingroup$

If I try to create a CSV file without the first line indicating the seperator, the resulting file is readable by Excel.

 Export["test2.csv",RandomInteger[{-5,5},{5,3}]]

But if you still want the first line, I would do something like this

Export["test3.csv", 
 "sep = ,\n" <> ExportString[RandomInteger[{-5, 5}, {5, 3}], "CSV"]
 , "Text"]
$\endgroup$
3
  • 1
    $\begingroup$ Sadly, with my language settings, Excel wants ";" as delimiters. I've tried to change that, but that wasn't successful. Thanks for the good help! $\endgroup$
    – Gabriel
    Jan 24, 2014 at 18:41
  • 3
    $\begingroup$ Ahh, okay. Another solution would be to export it as a "Table" but usint the FieldSparators option. Like Export["test.csv",RandomInteger[{-5,5},{5,3}],"Table","FieldSeparators"->" ; "] $\endgroup$
    – Jason B.
    Jan 24, 2014 at 18:58
  • 1
    $\begingroup$ Another approach might be to export comma separated, but with a "txt" extension. I think then Excel will pop up a box and let you tell it what the delimiter is. $\endgroup$
    – george2079
    Jan 24, 2014 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.