Message Boards Message Boards

0
|
4538 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Way to import formulas in a textfile in Wolfram Mathematica

Posted 11 years ago
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
3 Replies
Posted 11 years 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
Posted 11 years 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
Posted 11 years 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
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract