I have a function f[a]
. I run it through Table
to generate a huge list of values for later use in a C program as a lookup table. I want to export this to CSV so I can copy/paste it into my code editor and quickly turn it into a C array (all I'd have to do is wrap it in curly braces and give it a name):
Export["myfile.csv", Table[ f[a], {a, 0, 6} ], "CSV" ];
What I want is this:
0, 1, 2, 3, 4, 5, 6
and so on, but I end up with this:
0
1
2
3
4
5
6
Each entry is on a new line. What simple, obvious thing have I missed?
Thanks!
Re: Handling 'comma' in the data while writing to a CSV. So for data fields that contain a comma, you should just be able to wrap them in a double quote. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.
Some people's name use commas, for example Joe Blow, CFA. This comma breaks the CSV format, since it's interpreted as a new column. I've read up and the most common prescription seems to be replacing that character, or replacing the delimiter, with a new value (e.g. this|that|the, other ).
New LineLineIn computing, a line is a unit of organization for text files. A line consists of a sequence of zero or more characters, usually displayed within a single horizontal sequence.https://en.wikipedia.org › wiki › Line_(text_file)Line (text file) - Wikipedia Characters (Reference: https://en.wikipedia.org/wiki/Newline.) If you upload a CSV file that contains new line characters in the fields other than Text Area Field, it will be saved to the system but only Text Area Field preserves the new line if you edit the Asset.
Rules typical of these and other "CSV" specifications and implementations are as follows: CSV is a delimited data format that has fields/columns separated by the comma character and records/rows terminated by newlines.
You could export using the more general "Table" format and specify the line separators. For example:
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]
*)
You might also need to specify the "FieldSeparators"
option, it's default for the "Table"
format is "\t"
.
If your export object is a single list, you can avoid modifying its elements, and just wrap it in List
:
Export["myfile.csv", List @ Table[f[a], {a, 0, 6}]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With