convert string number to real number - ValueError: invalid literal for int() with base 10: '"2"'

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue May 27 22:02:32 EDT 2008


En Tue, 27 May 2008 13:00:05 -0300, David Jackson <davidj411 at gmail.com>  
escribió:

> i used the csv module and saved its contents to a list.
>
> ['Date', 'No.', 'Description', 'Debit', 'Credit']
> ['3/17/2006', '5678', 'ELECTRONIC PAYMENT', '', '11.45']
> ['3/04/2007', '5678', 'THE HOME DEPOT 263 SomeCity FL', '', '25.40']
>
>
> the credit/debit fields are strings.
> what should i have done within the CSV module to make numbers appear as
> numbers?
> how can i remove the quotes to make them numbers? i realize i posted a
> solution to this once before (same posting thread) but i am thinking  
> there
> is a better method.

What do you want to do with those empty strings? Convert them to zeros?  
Suppose your list is named `rows`:

for i,row in enumerate(rows):
     if not i: continue # skip titles
     row[3] = float(row[3] or 0)
     row[4] = float(row[4] or 0)

-- 
Gabriel Genellina




More information about the Python-list mailing list