0
$\begingroup$

Using:

Do[Write[OUTFILE, #] & @@ Through[{(#) &, Sin[#] &, Cos[#] &}[n]], {n, {1, 2, 3, 4}}]

results with file content:

{1, Sin[1], Cos[1]}
{2, Sin[2], Cos[2]}
{3, Sin[3], Cos[3]}
{4, Sin[4], Cos[4]}

But I would like to have:

1,Sin[1],Cos[1]
2,Sin[2],Cos[2]
3,Sin[3],Cos[3]
4,Sin[4],Cos[4]

And, additionally, when writing file paths, they are quoted. How to write path stripped of quotes?

$\endgroup$
1
  • 1
    $\begingroup$ Something like ExportString[RandomReal[1, {3, 4}], "CSV"]? $\endgroup$
    – Kuba
    Commented Nov 13, 2014 at 20:14

1 Answer 1

2
$\begingroup$

If you want to write one line at a time, use ExportString on each line, then WriteString:

 Do[
     WriteString[OUTFILE,
       ExportString[{ Through[{(#) &, Sin[#] &, Cos[#] &}[n]] }, "CSV"] <> "\n"],
            {n, {1, 2, 3, 4}}]

Generally you should save up the output data and Export it all at once though. Only write in the loop like that if for example the loop time is very long and you need the output always up to date.

$\endgroup$

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.