Maggie wrote:
code practice:
test = open ("test.txt", "r")
readData = test.readlines()
#set up a sum
sum = 0;
for item in readData:
sum += int(item)
print sum
test file looks something like this:
34
23
124
432
12
when i am trying to compile this it gives me the error: invalid
literal for int() with base 10
i know a lot of people get this and it usually means that you try to
cast a string into an integer and this string does not really contain
a “digit”..so I am just not sure how to correct it in this case...
You already have your specific answer, but you need a general strategy
also. When you have a problem processing data from a file, you should
ask: "Is the problem with the file data? Or is it with the subsequent
processing?". The answers come from two different test programs.
For the first:
print readData
#or
for item in readData: print repr(item)
This would have shown you that the file is not what you thought.
For the second:
readData = ['34', '23'] # etc
#read of program
and it that works, add '\n' to the end of each item.
Any decent programming editor will let you comment out blocks of text
without deleting them.
In other words, to debug, run simple experiments.
Terry Jan Reedy
--
http://mail.python.org/mailman/listinfo/python-list