Export a table to CSV with commas rather than newlines

I have a function f[a]. I run it through Tableto create a huge list of values ​​for later use in a C program as a lookup table. I want to export this to CSV so that I can copy / paste it into my code editor and quickly turn it into a C array (all I need to do is wrap it in braces and give it a name):

Export["myfile.csv", Table[ f[a], {a, 0, 6} ], "CSV" ];

I want it:

0, 1, 2, 3, 4, 5, 6

etc., but in the end I get the following:

0
1
2
3
4
5
6

Each entry is on a new line. What is the simple, obvious thing that I missed?

Thank!

+5
source share
4 answers

, "" . :

Export["myfile.csv", Table[f[a], {a, 0, 6}], "Table", "LineSeparators" -> ", "]

FilePrint["myfile.csv"]
(* Returns: 
f[0], f[1], f[2], f[3], f[4], f[5], f[6]
*)

"FieldSeparators", "Table" "\t".

+10

, List:

Export["myfile.csv", List @ Table[f[a], {a, 0, 6}]]
+6

, :

Export["myfile.csv", Transpose@Table[{f@a}, {a, 0, 6}], "CSV"]

, , . . .


klunky , , Mathematica . CSV :

Import["myfile.csv"]
Out[1]={{0}, {1}, {2}, {3}, {4}, {5}, {6}}

you see that Mathematica automatically fills each item into a list. Therefore, it must be filled in the list, one way or another, as in the answer of Mr.Wizard .

+4
source

How about using Partition?

f[x_] = Sin[x/10.];

Export["C:\\Users\\Sjoerd\\Desktop\\myfile.csv",
       Partition[Table[f[a], {a, 0, 600}], 30], 
       "CSV"
];

enter image description here

+3
source

All Articles