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
Write[file, ToString[expr, InputForm]]
to export your expression(s). Then useToExpression[StringDelete[ReadString["test.txt"], "\n" | "\r"]]
to get it back. As you can read inWriteString
, it usesOutputForm
, which is not suitable for parsing. That is why you have to export it toInputForm
which can be parsed again. $\endgroup$