Help with code?


It just outputs 1 loads of times

I want my code to loop through a file (which is saved as a .txt) and split it into sections which have the " : " after it. Then I want it to get all the numbers from column 6 to get subtract by column 5 and print it out afterward.

count = 4
count2 = 5

numbers = open("number.txt", "r")
number  = numbers.split(':')

for lines in number:

    number_1 = number[count]
    number_2 = number[count2]

    total = (int(number_1)) - (int(number_2))

    print total

    count = 3
    count2 = 4
    count3 = 5


Example of file - 

10 : 11 : 12 : 13 : 14 : 15 : 16 :

Help with code?

you can't add split directly on the file, you need to get to the lines first:

numbers = open("number.txt", "r")
number = numbers.readline()
print number.split(':')

or in your case, you have a loop:

numbers = open("number.txt", "r")
for lines in numbers:
   line = lines.split(':')


Thanks, I was about to ask for a loop


yea, i need to look up the loop syntax.

but the most important thing: you can't call split directly on the file, you need the lines, split() is designed to work for strings, not files


Thank you, that helped me a lot!


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.