0
$\begingroup$

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,):

Write[file,expr]; 
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]
NotebookClose[nbb];

The output is : iP==jP

and if I read the file it returns:

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

k>=kP , the first line only.

Also import a text is not evaluable

LF1 = Import[filename,"Text"];
Close[filename]; 

please help me as I need it. Thanks

$\endgroup$
2
  • 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
    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
    Oct 28, 2022 at 18:36

1 Answer 1

1
$\begingroup$

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]];
Close[file];

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];
$\endgroup$

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.