473,396 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to calculate a file of equations in python

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.

Thank you.

Mar 20 '07 #1
4 1408

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','' ))
Mar 20 '07 #2
On Mar 20, 1:03 am, Michael Bentley <rmc...@gmail.comwrote:
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/Coo.../Recipe/364469
Michele Simionato

Mar 20 '07 #3
"John" <Al************@gmail.comwrites:
I have a text file which contains math expression, like this
134
+234
+234
Erm, is this a homework assignment? Basicaly you need to identify all
the separate operands and operators and decide what to do with them.
I'm guessing you don't have to worry about operator precedence, for
example the rule that 2+3*5 is treated as 2+(3*5) because * has
precedence over +. Anyway the idea is keep a running total and
read through the input lines updating the total for each line.
Mar 20 '07 #4
r=''
for line in file : r = str(eval(r+line))

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.

Thank you.
Mar 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Klaus Jensen | last post by:
Hi! I am currently converting an ASP/SQL Server application to VB.Net/Access. The application performs calculations based on which products are selected. Therefore i need to be able to store...
5
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I...
8
by: vj | last post by:
Hi all, I want to solve the two equations u*tan(u)=w and u^2 + w^2=V^2, where V is a known constant, and u and w are the two unknowns to be determined. Please can someone suggest me how to...
6
by: LaundroMat | last post by:
Hi - I'm trying to calculate unique hash values for binary files, independent of their location and filename, and I was wondering whether I'm going in the right direction. Basically, the hash...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.