9
$\begingroup$

Given a pretty long expression, such as

a={-11 ψ^2 λ[1]+6 ψ λ[2],35 ψ^2 λ[2],11 ψ^2 λ[1]^2-11 ψ^2 λ[2]-6 ψ λ[1] λ[2],ψ^2 λ[1]^2-ψ^2 λ[2]-ψ λ[1] λ[2]+λ[2]^2,-λ[1]^3+2 λ[1] λ[2],-3 ψ λ[1]^3+6 ψ λ[1] λ[2]+λ[1]^2 λ[2]-λ[2]^2,-ψ λ[1]^3+λ[1]^4+2 ψ λ[1] λ[2]-3 λ[1]^2 λ[2]+λ[2]^2}

a >> tmp would output the expression of a to an external file tmp. But I don't like the format in this output. It breaks lines, and contains extra spaces between operators of "+-*/".

In fact, in Mathematica, selecting an expression, we could do (right click) > Copy as > Plain Text, and then we could paste it to any external text editor without any problem.

Is it possible to specify the output stream using this "Plain Text" format above?

$\endgroup$

6 Answers 6

10
$\begingroup$
Export["test.txt", {a}] 

works for me

"Plaintext" is already the default output form of Export. The { } around a prevents Export from seeing it as a series of arguments (a is a list in your example) that each have to be put on its own line.

$\endgroup$
3
  • $\begingroup$ +1 ... or at least is the output form derived from the file extension ".txt" which can, alternatively, be specified directly if needed - e.g. Export["test.sh",{a},"Text"] $\endgroup$ Sep 20, 2015 at 0:10
  • $\begingroup$ In some cases Exporting as "Text" can corrupt the exported Mathematica expression. The only safe format is "Package" but it adds the extra line (* Created with the Wolfram Language : www.wolfram.com *). $\endgroup$ Jun 24, 2016 at 14:01
  • $\begingroup$ Please see my answer for some workarounds. $\endgroup$ Jun 24, 2016 at 14:34
8
$\begingroup$

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]]]
$\endgroup$
2
  • $\begingroup$ This seems to no longer work for PutAppend in Mathematica 10. $\endgroup$
    – Taiki
    Mar 21, 2015 at 2:16
  • 3
    $\begingroup$ There is a bug introduced in v.10: SetOptions no longer affects the behavior of OpenWrite and OpenAppend. $\endgroup$ Mar 22, 2015 at 14:51
4
$\begingroup$

Perhaps you could use Export instead of Put?

out = StringReplace[ToString[InputForm[a]], {" " | "\n" -> ""}]
Export["out.txt", out]
$\endgroup$
3
$\begingroup$

Try using Export and Import.

Make sure to include "Data" as the second argument of Import.

SetDirectory[$TemporaryDirectory];
Export["myfile.txt", a]
in = Import["myfile.txt", "Data"]

data


If you really want to see the Greek letters spelled out you can then use

FullForm[in]

but this will also return List[] instead of {}.

$\endgroup$
3
$\begingroup$

I can't get Alexey's solution to work on Mathematica 10, so I use this custom function to output simple strings in lieu of PutAppend:

myPutAppend[string_String, pathtofile_] := Module[
  {f},
  f = OpenAppend[pathtofile];
  WriteString[f, string <> "\n"];
  Close[f]
]

The \n bit is needed because WriteString does not automatically add it to the end of the line like the original PutAppend.

You can also have

myPut[string_String, pathtofile_] := Module[
  {f},
  f = OpenWrite[pathtofile];
  WriteString[f, string <> "\n"];
  Close[f]
]

for the equivalent of Put.

$\endgroup$
2
$\begingroup$

Using undocumented FrontEnd`ExportPacket command you can get exactly the same formatting as with Copy As ► Plain Text (i.e. without the line breaks and extra spaces):

s = First[FrontEndExecute[FrontEnd`ExportPacket[Cell[BoxData[ToBoxes[a]]], "PlainText"]]]

{-11 \[Psi]^2 \[Lambda][1]+6 \[Psi] \[Lambda][2],35 \[Psi]^2 \[Lambda][2],11 \[Psi]^2 \[Lambda][1]^2-11 \[Psi]^2 \[Lambda][2]-6 \[Psi] \[Lambda][1] \[Lambda][2],\[Psi]^2 \[Lambda][1]^2-\[Psi]^2 \[Lambda][2]-\[Psi] \[Lambda][1] \[Lambda][2]+\[Lambda][2]^2,-\[Lambda][1]^3+2 \[Lambda][1] \[Lambda][2],-3 \[Psi] \[Lambda][1]^3+6 \[Psi] \[Lambda][1] \[Lambda][2]+\[Lambda][1]^2 \[Lambda][2]-\[Lambda][2]^2,-\[Psi] \[Lambda][1]^3+\[Lambda][1]^4+2 \[Psi] \[Lambda][1] \[Lambda][2]-3 \[Lambda][1]^2 \[Lambda][2]+\[Lambda][2]^2}

You can directly copy it into the clipboard with CopyToClipboard, and WriteString or Export as "Text" with safety:

CopyToClipboard@s

WriteString["test.txt", s, "\n"]

Export["test.txt", s]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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