I have a series of expression which is added one by one, not all at once, to a file (OUT.nb). for some of them that has POWER format (a^2 + b^3 ), WriteString cannot write it correct. Also I tried Write but it adds every expression in one single line. The problem is after all writing completion, I need to read/evaluate the OUT.nb file but with those problems it does not work for me. If the content could be written all in a single line then it would be great and works for me.

So I tried multiple ways for my code which is called a few times and writes into that file(in each iteration one expression is written to the file,):

WriteString[file," && "];

the content of file is (writeString does not write power expression correctly),

 k >= kP
 && 7^k*(-7 - 6*y) == 7^kP*(-7 - 6*yP)
 &&  Not[ iP != jP ]   

so far so good but when I want to read it or evaluate it it does not work fine:

nbb = NotebookOpen[
   FileNameJoin[{Directory[], "MathematicaOutputInvR.nb"}], 
   Visible -> False];
LF1 = NotebookEvaluate[nbb, InsertResults -> True]

The output is : iP==jP

and if I read the file it returns:

filename = FileNameJoin[{Directory[], "MathematicaOutputInvR.nb"}];
LF1 = Read[filename]

k>=kP , the first line only.

Also import a text is not evaluable

LF1 = Import[filename,"Text"];

please help me as I need it. Thanks

  • 1
    $\begingroup$ Use Write[file, ToString[expr, InputForm]] to export your expression(s). Then use ToExpression[StringDelete[ReadString["test.txt"], "\n" | "\r"]] to get it back. As you can read in WriteString, it uses OutputForm, which is not suitable for parsing. That is why you have to export it to InputForm which can be parsed again. $\endgroup$
    – Domen
    Commented Oct 28, 2022 at 18:28
  • $\begingroup$ thanks , but where and when should I use ToExpression? you mean instead of Read? could you give me the full answer as I am new to Wolfram. appreciate it. $\endgroup$
    – Azzurro94
    Commented Oct 28, 2022 at 18:36

1 Answer 1


As you can read in the documentation for WriteString, it uses OutputForm of the expression, which is not suitable for importing. That is why you have to export it with InputForm which can be parsed again. To convert a string into an expression, use ToExpression.

expr1 = a^2 > b;
expr2 = 2 b < c^3;
expr3 = d == e;

filename = "test.txt";

file = File[filename];
WriteString[file, ToString[expr1, InputForm]];
WriteString[file, "&&"];
WriteString[file, ToString[expr2, InputForm]];
WriteString[file, "&&"];
WriteString[file, ToString[expr3, InputForm]];

expr = ToExpression[ReadString[filename]]

(* a^2 > b && 2 b < c^3 && d == e *)

You could also first build the whole expression directly, and export it just once with Export.

expr = And@@ {expr1, expr2, expr3};
Export[filename, expr];

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.