Group Abstract Group Abstract

Message Boards Message Boards

Way to import formulas in a textfile in Wolfram Mathematica

GROUPS:
Hi,

Is there a way to import formulas in a textfile into Wolfram Mathematica?
I have a text file that has in every line a formula to calculate.  So is there a way to import. ala .csv file or so?

Example for this text file:
453645 + 567323 =
3453454 + (3453454 * 5756757) =
....
POSTED BY: Brody White
Answer
1 year ago
In[1]:= strings = Import["formula.txt"];
ToExpression[StringDrop[StringSplit[strings, "=\n"], -1]]

Out[2]= {1020968, 19880698942132}

where formula.txt contains the two lines you showed in your example. You may need to provide a path to your text file, depending on location.

This can also be
In[3]:= ToExpression[StringDrop[StringSplit[Import["formula.txt"], "=\n"], -1]]

Out[3]= {1020968, 19880698942132}
POSTED BY: Bill Simpson
Answer
1 year ago
Dear Sir,

Thank you for your answer.

Yes, this works.
Second: Is there a way to write the formula left on the sum too? Is there a way to make a new line when there is a new sum?
ex. Out[3]= {453645 + 567323 = 1020968
3453454 + (3453454 * 5756757) = 19880698942132
....
...}
POSTED BY: Brody White
Answer
11 months ago
In[1]:= strings = Import["formula.txt"];
f[s_] := s <> "=" <> ToString[ToExpression[s]];
TableForm[Map[f, StringDrop[StringSplit[strings, "=\n"], -1]]]

Out[3]=
453645 + 567323=1020968
3453454 + (3453454 * 5756757) =19880698942132
POSTED BY: Bill Simpson
Answer
11 months ago