# How to output an expression to an external file in “plain text” format without breaking lines?

Given a pretty long expression, such as

  a={-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}


"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?

-

## 3 Answers

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.

-

Perhaps you could use Export instead of Put?

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

-

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"]


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 {}.

-