Original answer
You can stay with Put
using the method I showed here for PutAppend
:
SetOptions[OpenWrite, PageWidth -> Infinity];
a >> tmp
This method is especially useful in the case of PutAppend
because it allows you to maintain a running log file with results of intermediate computations with one expression per line.
UPDATE: a bug introduced in version 10 (fixed in version 11.3)
There is a bug introduced in version 10: SetOptions
no longer affects the behavior of OpenWrite
and OpenAppend
:
SetOptions[OpenWrite, PageWidth -> Infinity];
str = OpenWrite["log.txt"]; Write[str, Table[x, {50}]];
Close[str];
Import["log.txt"] // FullForm
"{x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
x, x, \n x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
x, x, x, x, x}"
As one can see, the newline character \n
is embedded despite the fact that OpenWrite
is set to have the PageWidth -> Infinity
option system-wide. A workaround is to explicitly set PageWidth -> Infinity
for the stream:
str = OpenWrite["log.txt", PageWidth -> Infinity];
Write[str, Table[x, {50}]];
Close[str];
Import["log.txt"] // FullForm
DeleteFile["log.txt"]
"{x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x,
x, x, x, x}"
Another workaround is to Export
as "Text"
but not directly: direct exporting can corrupt the Mathematica expression. Instead one should explicitly convert the expression into the corresponding InputForm
string as follows:
Export["test.txt", ToString[a, InputForm]]
An alternative is to pre-wrap the expression both by OutputForm
and InputForm
as follows:
Export["test.txt", OutputForm[InputForm[a]]]