I'm a Mathematica newbie and I'm trying to append some data to a file but I am experiencing some weird behaviour when using PutAppend.

Here is what I am doing:

AbsVal[T_, numofterms_, a_] := Abs[B2[T, numofterms, a]/numint[T, a]][[1]]

For[n = 1, n < 500, n += 5, PutAppend[Flatten[{n, AbsVal[t, n, a]}], "myfile.txt"]]

I'll now give you example of the outputs. For

AbsVal[t, n, a]

I get an output with the following format:


But in my file I see

{6, 3.0900615799443788409187442259678172695491469841`15.954589770191005*^-416}
{11, 2.1455743830188223575511135886399984856855`15.954589770191005*^-403}
{21, 6.0066411663977041046546546698428746195726683`15.954589770191005*^-380}
{26, 5.31062408165803191323563134185348816`15.954589770191005*^-369}
{36, 3.1567662580039551788994831970476421271`15.954589770191005*^-348}
{46, 1.25574842187340348082241748599920258043`15.954589770191005*^-328}
{51, 3.450109407850428362698276923292493910486398948`15.954589770191005*^-319}
{56, 5.8363462554023196163570433861000185804`15.954589770191005*^-310}
{61, 6.330104189030497*^-301}
{66, 4.55439511956804*^-292}
{71, 2.237735694265989*^-283}
{76, 7.698747588846684*^-275}
{81, 1.895566134531356*^-266}

which I think is very weird because:

  • In the first line, there is a linebreak between 1, and 6.6343987(blah blah blah)
  • Sometimes I have that weird `15.954589770191005 term with that weird apostrophe (which I have no idea what it means)
  • Also sometime I get a backslash \ when the line gets too long

As you have noticed, this is all very weird to me!!

What I'm aiming for is for a consistent output like the one we see in the last five lines. What I was REALLY aiming for is to have a csv format as output (without the curly brackets)

What am I doing wrong? or what do I need to configure to fix this?

Thank you very much!

  • 1
    $\begingroup$ @T.P.Vasconcelos No worry, you cannot directly delete the comment or answer of others by clicking on them :) It may have been removed by sblom when you were voting up. $\endgroup$
    – user31159
    Nov 10 '16 at 1:03
  • 1
    $\begingroup$ @T.P.Vasconcelos No, not even on your own question. See edited comment above for the second question. $\endgroup$
    – user31159
    Nov 10 '16 at 1:06
  • 1
    $\begingroup$ Use Export. Put will write Mathematica expressions, and you don't want Mathematica syntax in your output. The usual way to export data is to first generate all of it, then Export a whole list in one go. Always do this unless you know why you need to append step by step. If you're new to Mathematica forget about For completely, as it will be counterproductive to your learning. For procedural loops use Do instead, or While when needed. But try to avoid procedural loops. The right way to deal with your example is to generate data with Table, then Export. $\endgroup$
    – Szabolcs
    Nov 10 '16 at 7:59
  • 1
    $\begingroup$ The link above is about appending in CSV format, for the rare cases when that is necessary. But do consider something like this first: result = Table[{n, AbsVal[t, n, a]}, {n, 1, 50, 5}]; Export["output.csv", result]. $\endgroup$
    – Szabolcs
    Nov 10 '16 at 12:57
  • 1

Found the answer to my question!!

For[n = 1, n < 50, n += 5, {n, FortranForm[AbsVal[t, n, a]]} >>> "myfile.txt"]

which appends the following output to myfile.txt

{1, 6.634398712795079e-430}
{6, 3.090061579944379e-416}
{11, 2.145574383018822e-403}
{16, 2.272995046809765e-391}
{21, 6.006641166397704e-380}
{26, 5.310624081658032e-369}
{31, 1.902243066216567e-358}
{36, 3.156766258003955e-348}
{41, 2.679793619146347e-338}
{46, 1.255748421873403e-328}

I'm happy with this for now!


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