1

When I run my code below I get a: ValueError: invalid literal for int() with base 10: '0.977759164126' but i dont know why

file_open = open("A1_B1_1000.txt", "r")
file_write = open ("average.txt", "w")

line = file_open.readlines()
list_of_lines = []
length = len(list_of_lines[0])
total = 0


for i in line:
    values = i.split('\t')
    list_of_lines.append(values)

count = 0
for j in list_of_lines:
    count +=1

for k in range(0,count):
    print k
    list_of_lines[k].remove('\n')



for o in range(0,count):
    for p in range(0,length):
        print list_of_lines[p][o]
        number = int(list_of_lines[p][o])    
        total + number
    average = total/count
    print average

My text file looks like:

0.977759164126 0.977759164126 0.977759164126 0.977759164126 0.977759164126
0.981717034466 0.981717034466 0.981717034466 0.981717034466 0.98171703446

The data series is in rows and the values are tab delimited in the text file. All the rows in the file are the same length.

The aim of the script is to calculate the average of each column and write the output to a text file.

2 Answers 2

9

int() is used for integers (numbers like 7, 12, 7965, 0, -21233). you probably need float()

Sign up to request clarification or add additional context in comments.

2 Comments

D'oh such a simple answer. My bad
Haha it wouldn't let me I had to wait 8 mins :(
0

Python is limited on handling floating points. These all work fine here but for longer ones as well as arithmetic you are going to want to use the Decimal module.

import Decimal
result = Decimal.Decimal(1)/Decimal.Decimal(5)
print result

Link to the documentation http://docs.python.org/2/library/decimal.html

Try typing in 1.1 into IDLE and see what your result is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.