Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Running Sum script

Reply
Thread Tools

Running Sum script

 
 
Jul
Guest
Posts: n/a
 
      09-04-2009
hello,

I have a .txt file that is in this format --

12625
17000
12000
14500
17000
12000
17000
14500
14500
12000
....and so on...

i need to create a python script that will open this file and have a
running sum until the end of file.

it sounds really simple its just for some reason i am having problem
with it.

i would really appreciate your help
 
Reply With Quote
 
 
 
 
David Smith
Guest
Posts: n/a
 
      09-04-2009
Jul wrote:
> hello,
>
> I have a .txt file that is in this format --
>
> 12625
> 17000
> 12000
> 14500
> 17000
> 12000
> 17000
> 14500
> 14500
> 12000
> ...and so on...
>
> i need to create a python script that will open this file and have a
> running sum until the end of file.
>
> it sounds really simple its just for some reason i am having problem
> with it.
>
> i would really appreciate your help


It is really simple. Can you post the code you've written so far?

--David
 
Reply With Quote
 
 
 
 
Jul
Guest
Posts: n/a
 
      09-04-2009
On Sep 4, 2:21 pm, Stephen Fairchild <(E-Mail Removed)> wrote:
> Jul wrote:
> > hello,

>
> > I have a .txt file that is in this format --

>
> > 12625
> > 17000
> > 12000
> > 14500
> > 17000
> > 12000
> > 17000
> > 14500
> > 14500
> > 12000
> > ...and so on...

>
> > i need to create a python script that will open this file and have a
> > running sum until the end of file.

>
> Untested:
>
> with open("numbers.txt", "r") as f:
> print sum(int(x) for x in f)
> --
> Stephen Fairchild


thats what i have so far --

#!/usr/bin/python

import os.path

#open up the file
formisanoOpen = open("formisano_num.txt", "r")

#read in all the data into a list
readData = formisanoOpen.readLines()

#set up a sum
sum = 0;

#begin a loop
for trial in readData:

#the next line is indented (YA doesn't indent)
sum += int(trial)

#loop is over, so unindent
#report the sum
print sum


end

but it doesnt want to run for some reason....
 
Reply With Quote
 
David Smith
Guest
Posts: n/a
 
      09-04-2009
Jul wrote:
> On Sep 4, 2:21 pm, Stephen Fairchild <(E-Mail Removed)> wrote:
>> Jul wrote:
>>> hello,
>>> I have a .txt file that is in this format --
>>> 12625
>>> 17000
>>> 12000
>>> 14500
>>> 17000
>>> 12000
>>> 17000
>>> 14500
>>> 14500
>>> 12000
>>> ...and so on...
>>> i need to create a python script that will open this file and have a
>>> running sum until the end of file.

>> Untested:
>>
>> with open("numbers.txt", "r") as f:
>> print sum(int(x) for x in f)
>> --
>> Stephen Fairchild

>
> thats what i have so far --
>
> #!/usr/bin/python
>
> import os.path
>
> #open up the file
> formisanoOpen = open("formisano_num.txt", "r")
>
> #read in all the data into a list
> readData = formisanoOpen.readLines()
>
> #set up a sum
> sum = 0;
>
> #begin a loop
> for trial in readData:
>
> #the next line is indented (YA doesn't indent)
> sum += int(trial)
>
> #loop is over, so unindent
> #report the sum
> print sum
>
>
> end
>
> but it doesnt want to run for some reason....


.... ok ... what do you get. If it's an error, please post the stack
trace. Please help us help you -- provide details.

--David
 
Reply With Quote
 
Rami Chowdhury
Guest
Posts: n/a
 
      09-04-2009
Could you let us know what kind of error you are getting?

I don't know if this is your error, but this line won't run:

> readData = formisanoOpen.readLines()


Since Python is case-sensitive, you would need a lower-case 'l' in
'readlines()' -- perhaps that would solve your problem?

On Fri, 04 Sep 2009 11:26:06 -0700, Jul <(E-Mail Removed)> wrote:

> On Sep 4, 2:21 pm, Stephen Fairchild <(E-Mail Removed)> wrote:
>> Jul wrote:
>> > hello,

>>
>> > I have a .txt file that is in this format --

>>
>> > 12625
>> > 17000
>> > 12000
>> > 14500
>> > 17000
>> > 12000
>> > 17000
>> > 14500
>> > 14500
>> > 12000
>> > ...and so on...

>>
>> > i need to create a python script that will open this file and have a
>> > running sum until the end of file.

>>
>> Untested:
>>
>> with open("numbers.txt", "r") as f:
>> print sum(int(x) for x in f)
>> --
>> Stephen Fairchild

>
> thats what i have so far --
>
> #!/usr/bin/python
>
> import os.path
>
> #open up the file
> formisanoOpen = open("formisano_num.txt", "r")
>
> #read in all the data into a list
> readData = formisanoOpen.readLines()
>
> #set up a sum
> sum = 0;
>
> #begin a loop
> for trial in readData:
>
> #the next line is indented (YA doesn't indent)
> sum += int(trial)
>
> #loop is over, so unindent
> #report the sum
> print sum
>
>
> end
>
> but it doesnt want to run for some reason....




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
 
Reply With Quote
 
Juli Dolzhenko
Guest
Posts: n/a
 
      09-04-2009
On Sep 4, 2:52 pm, "Rami Chowdhury" <(E-Mail Removed)> wrote:
> Could you let us know what kind of error you are getting?
>
> I don't know if this is your error, but this line won't run:
>
> > readData = formisanoOpen.readLines()

>
> Since Python is case-sensitive, you would need a lower-case 'l' in
> 'readlines()' -- perhaps that would solve your problem?
>
>
>
> On Fri, 04 Sep 2009 11:26:06 -0700, Jul <(E-Mail Removed)> wrote:
> > On Sep 4, 2:21 pm, Stephen Fairchild <(E-Mail Removed)> wrote:
> >> Jul wrote:
> >> > hello,

>
> >> > I have a .txt file that is in this format --

>
> >> > 12625
> >> > 17000
> >> > 12000
> >> > 14500
> >> > 17000
> >> > 12000
> >> > 17000
> >> > 14500
> >> > 14500
> >> > 12000
> >> > ...and so on...

>
> >> > i need to create a python script that will open this file and have a
> >> > running sum until the end of file.

>
> >> Untested:

>
> >> with open("numbers.txt", "r") as f:
> >> print sum(int(x) for x in f)
> >> --
> >> Stephen Fairchild

>
> > thats what i have so far --

>
> > #!/usr/bin/python

>
> > import os.path

>
> > #open up the file
> > formisanoOpen = open("formisano_num.txt", "r")

>
> > #read in all the data into a list
> > readData = formisanoOpen.readLines()

>
> > #set up a sum
> > sum = 0;

>
> > #begin a loop
> > for trial in readData:

>
> > #the next line is indented (YA doesn't indent)
> > sum += int(trial)

>
> > #loop is over, so unindent
> > #report the sum
> > print sum

>
> > end

>
> > but it doesnt want to run for some reason....

>
> --
> Rami Chowdhury
> "Never attribute to malice that which can be attributed to stupidity" --
> Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)


in the terminal i get a very strange "permission denied" error that
might not have anything to do with the code. I checked permissions for
the file and they are set to "read and write" so, again, I am really
not sure what going wrong.
 
Reply With Quote
 
Maggie
Guest
Posts: n/a
 
      09-04-2009
On Sep 4, 4:37 pm, Tobiah <(E-Mail Removed)> wrote:
> > in the terminal i get a very strange "permission denied" error that might
> > not have anything to do with the code. I checked permissions for the file
> > and they are set to "read and write" so, again, I am really not sure what
> > going wrong.

>
> Try:
>
> python myfile
>
> Or
>
> chmod +x myfile
> ./myfile


try it where? code or terminal?

thanks so much?
 
Reply With Quote
 
Tobiah
Guest
Posts: n/a
 
      09-04-2009


> in the terminal i get a very strange "permission denied" error that might
> not have anything to do with the code. I checked permissions for the file
> and they are set to "read and write" so, again, I am really not sure what
> going wrong.


Try:

python myfile

Or

chmod +x myfile
./myfile
 
Reply With Quote
 
Rami Chowdhury
Guest
Posts: n/a
 
      09-04-2009
> try it where? code or terminal?

Please try these in the terminal -- the permission denied error may be due
to your shell not being able to execute the Python script, instead of your
Python script not being able to open the data file.

On Fri, 04 Sep 2009 13:37:10 -0700, Maggie <(E-Mail Removed)> wrote:

> On Sep 4, 4:37 pm, Tobiah <(E-Mail Removed)> wrote:
>> > in the terminal i get a very strange "permission denied" error that

>> might
>> > not have anything to do with the code. I checked permissions for the

>> file
>> > and they are set to "read and write" so, again, I am really not sure

>> what
>> > going wrong.

>>
>> Try:
>>
>> python myfile
>>
>> Or
>>
>> chmod +x myfile
>> ./myfile

>
> try it where? code or terminal?
>
> thanks so much?




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
 
Reply With Quote
 
Guest
Posts: n/a
 
      09-04-2009
On Sep 4, 4:37 pm, Maggie <(E-Mail Removed)> wrote:
> On Sep 4, 4:37 pm, Tobiah <(E-Mail Removed)> wrote:
>
> > > in the terminal i get a very strange "permission denied" error that might
> > > not have anything to do with the code. I checked permissions for the file
> > > and they are set to "read and write" so, again, I am really not sure what
> > > going wrong.

>
> > Try:

>
> > python myfile

>
> > Or

>
> > chmod +x myfile
> > ./myfile

>
> try it where? code or terminal?
>
> thanks so much?


fantastic...! thank you so much..now i finally have my errors - which
are the following --

File "running_sum_formisano.py", line 18
sum+= int(trial)
^
IndentationError: expected an indented block

how would this be fixed?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Block execution of script while same script is already running MrBanabas@googlemail.com Ruby 2 12-17-2008 06:03 PM
Something like cumulative or running sum papaja ASP .Net 3 02-12-2006 03:32 PM
GridView -Running sum NewTel ASP .Net 2 02-09-2006 08:06 PM
modify a long-running python script while it is running? Benjamin Rutt Python 2 12-20-2005 01:42 PM
Calculate Running sum of 4 text boxes on client side script? Davisro ASP .Net 2 04-20-2004 08:18 PM



Advertisments