How to calculate a file of equations in python
Michele Simionato
michele.simionato at gmail.com
Tue Mar 20 06:14:00 CET 2007
On Mar 20, 1:03 am, Michael Bentley <rmc... at gmail.com> wrote:
> On Mar 19, 2007, at 11:48 PM, John wrote:
>
> > Hi,
> > I have a text file which contains math expression, like this
> > 134
> > +234
> > +234
>
> > (i.e. an operation (e.g. '+) and then a number and then a new line).
>
> > Can you please tell me what is the easiest way to calculate that file?
> > for example the above example should be = 134 + 234 + 234 = 602.
>
> Easiest?
>
> eval(' '.join(open(fileName).readlines()).replace('\n',''))
Or use one of the many safe_eval recipes out there, for instance
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/364469
Michele Simionato
More information about the Python-list
mailing list