[Tutor] manipulting CSV files

Kent Johnson kent37 at tds.net
Fri Jan 8 16:07:43 CET 2010


On Fri, Jan 8, 2010 at 9:54 AM, Lowell Tackett <lowelltackett at yahoo.com> wrote:

> Now, perhaps you can you shed some light on this problem--I'm trying to do some simple arithmetic with two of the retrieved values, and get this error:
>
>>>> coord = csv.reader(open('true_coord', 'rb'), skipinitialspace = True)
>>>> for line in coord:
> ....  if line[0] == '1001':
> ....   print line
> ....
> ['1001', '342821.71900', '679492.08300', '0.00000', '']
> ....   print (int(line[1]) + int(line[2]))
> ....
> Traceback (most recent call last):
>  File "<stdin>", line 3, in <module>
> ValueError: invalid literal for int() with base 10: '342821.71900'

Well, it's right - that is not a valid integer. What do you want to do
with it? You could convert it to a number with float(line[2]) or get
just the integer part with int(line[2].split('.')[0]).

Kent


More information about the Tutor mailing list