hi I want to know the easyest way to readline() or readlines() or read()
a file so far I have this

>>> file = open( 'write_total.txt', 'r' )
>>> file.readlines()
['Line 1\n', 'Line 2\n', 'Line 3\n', 'Line 1\n', 'Line 2\n', 'Line 3\n']
>>>

I am not sure why this repeats line1 line2 line3 then line 1 line 2 line 3 there is 6 lines in the file.
but anyway if that does not matter...
How can I see the data such as $10.00 and not 'Line 1\n'....
How can I sum each line until it equals $60.00
Thanks for any help.

$10.00 
$10.00 
$10.00 
$10.00 
$10.00 
$10.00 

One way,you may need to break it up if list comprehension are something new.
List comprehension is very common to use in Python.

with open('number.txt') as f:
    lst = [i.strip() for i in f]
    print lst
    print [float(i.split('$')[1]) for i in lst] #split out $ and make float
    print sum(float(i.split('$')[1]) for i in lst) #sum upp all numbers

'''Output-->
['$10.00', '$10.00', '$10.00', '$10.00', '$10.00', '$10.00']
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0]
60.0
'''

Edited 2 Years Ago by snippsat

ok thanks so much I will try this and get back to you.
print [float(i.split('$')[1])#########for i in lst]
I got a syntax error where I put the #######
I am not sure what the first line of code
with open('number.txt') as f:
I am using python33

Edited 2 Years Ago by trade19: addition

can you help me doing the same thing using the code below?
also in my version
As it is the code below wont work

print ('won't work unless I use') parentheses

As it is the code below wont work 'to the point of helping me.
It just prints out 



$10.00........or line1 line2 ..

.....

 totals= open("C:write_it.txt", "r")
    totals.readline()
    for line in totals:
    print(line)
    totals.close()


FILE:    

$10.00 
$10.00 
$10.00 
$10.00 
$10.00 
$10.00 
$10.00  

Edited 2 Years Ago by trade19: addition

print [float(i.split('$')[1])#########for i in lst]
I got a syntax error where I put the #######

In Python 3 print is a function so you need ().

#Python 3
with open('number.txt') as f:
    lst = [i.strip() for i in f]
    print(lst)
    print([float(i.split('$')[1]) for i in lst]) #split out $ and make float
    print(sum(float(i.split('$')[1]) for i in lst)) #sum upp all numbers

I am not sure what the first line of code

with open() is the prefered way to use.
It dos some cleaing up and close file object for you.

can you help me doing the same thing using the code below?
also in my version

totals = open('number.txt')
total_lst = totals.readlines() #readlines not readline
print(total_lst)

for line in total_lst:
    print(line)
totals.close()

Next split() and make float and sum up.

totals = open('number.txt')
total_lst = totals.readlines() 
number_lst = []
for line in total_lst:
    line = line.strip().split('$')
    number_lst.append(float(line[1]))

print(number_lst) #[10.0, 10.0, 10.0, 10.0, 10.0, 10.0]
print(sum(number_lst)) #60.0
totals.close()

Or try this,just iterate over file object and see what you get.
So really no need for readlines() at all,in my example or your's.

totals = open('number.txt')
for line in totals:
    print(line.strip())

Edited 2 Years Ago by snippsat

also i forgot to mention each time I run the program a new amount is added to the file like this:

$10.00 
$8.00 
$10.00 
$6.00
$10.00 
$10.00 
$8.00 
$10.00 
$6.00
$10.00 
$10.00 
$8.00 
$10.00 
$6.00
$10.00 

I got this error:

Traceback (most recent call last):
  File "C:\Python33\keep2.py", line 34, in <module>
    number_lst.append(float(line[1]))
IndexError: list index out of range

this is the code I used:

totals = open('write_it.txt')
total_lst = totals.readlines()
number_lst = []
for line in total_lst:

    line=line.strip().split('$')
    number_lst.append(float(line[1]))
    print(number_lst) #[10.0, 10.0, 10.0, 10.0, 10.0, 10.0]
    print(sum(number_lst)) #60.0
totals.close()

I got this error:

My number.txt is just a copy from your first post.

$10.00 
$10.00 
$10.00 
$10.00 
$10.00 
$10.00 

this is the code I used:

You have changes indentation by moving print lines,please do not change code when you test.
Indentation in Python is very important.

also i forgot to mention each time I run the program a new amount is added to the file like this:

The format is the the same,so it should work and sum up as i showed.

Edited 2 Years Ago by snippsat

and this is the file:
$10.00
$8.00
$10.00
$6.00
$10.00
$10.00

and this is the file:

Running fine for me in both my code example,and i run code in Python 3.

#Python 3
totals = open('number.txt') #just copy,paste and save to number.txt.
number_lst = []
for line in totals:
    line = line.strip().split('$')
    number_lst.append(float(line[1]))

print(number_lst) #[10.0, 8.0, 10.0, 6.0, 10.0, 10.0]
print(sum(number_lst)) #54.0
totals.close()

Edited 2 Years Ago by snippsat

Not sure if this will help me but....This is the file: This how the program writes the $10.00 too the file.
Iam also using notepad
how does it work without "C:\number.txt"mine won't woek without the C:\ part
and number_lst = []I needed to change to number_lst=[]
$10.00
$8.00
$10.00
$6.00
$10.00
$10.00

Edited 2 Years Ago by trade19: addition

Iam also using notepad
how does it work without "C:\number.txt"mine won't work without the C:\ part
I also needed to change

number_lst = []

I needed to change to

number_lst=[]

and

line=line.strip().split('$')

No matter what I do I get: IndexError: list index out of range

The number.txt file looks like this:

'''Output-->
['$10.00', '$10.00', '$10.00', '$10.00', '$10.00', '$10.00']
[10.0, 10.0, 10.0, 10.0, 10.0, 10.0]
60.0
'''

Edited 2 Years Ago by trade19: addition

Not sure if this will help me but....This is the file: This how the program writes the $10.00 too the file.

What to you want,sum up all number as you postet before?
Show exact output you want.

how does it work without "C:\number.txt"mine won't woek without the C:\ part

Running code(.py) in same folder as number.txt,then no path is needed.
Never use single \ in filename "C:\number.txt" wrong.
C:\\number.txt ok, r"C:\number.txt" raw string ok,or "C:/number.txt".

Edited 2 Years Ago by snippsat

This is the part that writes to the fileor appends rather:

totals = open("C:\\write_it.txt", "a")
        totals.write('$%.2f \n'  % totalc)
        totals.close()

I can try removing the

\n

This is what is appended to the file:
$10.00
$8.00
$10.00
$6.00
$10.00
$10.00
without the

\n



$10.00 $8.00 $10.00 $6.00 $10.00 $10.00 

Edited 2 Years Ago by trade19: addition

Ok I put all you gave me toghether like this and it all works fine.
Thanks snipsat

num=1
number_lst=[]


with open("C:\\write_it.txt", "r") as totals:
    for line in totals:

        print('% 04d %s' %(num, line),end = '')        
        line=line.strip().split('$')
        number_lst.append(float(line[1]))
        print(number_lst, '\n')
        print(sum(number_lst)) #54.0

        num = num+1

totals.close()

The only issue I have is to change the list.
The result equals say: 54.0 but I can't seem to find what represents the amount of 54.0 I tried type and print(number_lst) but it comes back as a list or 10.0 How can I change it back to something, I can use to display, like a float that equals $54.00 varriable? and not a list?
I want to use this number in a Frame for display only reasons.
I don't need help on how to put it in the Frame.
Do I need to use .format?
Thanks

Edited 2 Years Ago by trade19: addition

Do I need to use .format?

Maybe this help.

>>> lst = ['$10.00', '$8.00', '$10.00', '$6.00', '$10.00', '$10.00']
>>> new_lst = [float(i.split('$')[1]) for i in lst]
>>> new_lst
[10.0, 8.0, 10.0, 6.0, 10.0, 10.0]
>>> total = sum(new_lst)
>>> total
54.0
>>>
>>> print('Sum of lst is ${}'.format(total))
Sum of lst is $54.0
>>>
>>> print('The sum of this list: {} is ${}'.format(lst, total))
The sum of this list: ['$10.00', '$8.00', '$10.00', '$6.00', '$10.00', '$10.00'] is $54.0

Edited 2 Years Ago by snippsat

ok I will check it

Edited 2 Years Ago by trade19: addition

I still can't get rid of this error
is it due to eof ? or end of line?

Traceback (most recent call last):
  File "C:\Python33\testkill.py", line 22, in <module>
    number_lst.append(float(line[1]))
IndexError: list index out of range

Iam getting the output totals of 76.0 and I think that is the 009 item counted. I can't seem to get out of the Index Error.

So it wont jump onto this part:

aApp=Tk()
aApp.geometry('650x450+185+185')
aApp.title("LOTTO")    

It just gives the Index error and stops at the>>>| and it is blinking.

76.0
 009  
Traceback (most recent call last):
  File "C:\Python33\testkill.py", line 12, in <module>
    number_lst.append(float(line[1]))
IndexError: list index out of range

I don't know how but it finnaly got to the point where it all works perfect.
Thanks for all your help

num=1
number_lst=[]
with open("C:\\write_it.txt", "r") as totals:
    for line in totals:



    print('% 04d %s' %(num, line),end = '')        
    line=line.strip().split('$')
    number_lst.append(float(line[1]))
    print(number_lst, '\n')
    lst=(sum(number_lst))


    num=num+1
    Label(aApp,text="Total purchases are:$%.2s\n" %  lst, fg = 'red',font=('times', 10, 'bold')).grid(row=0,column=1)
This article has been dead for over six months. Start a new discussion instead.