strings (dollar.cents) into floats

J. Cliff Dyer jcd at sdf.lonestar.org
Thu Aug 30 17:37:57 CEST 2007


Gary Herron wrote:
> luca bertini wrote:
>   
>> Hi,
>>
>> i have strings which look like money values (ie 34.45)
>> is there a way to convert them into float variables?
>> everytime i try I get this error: "numb = float(my_line) ValueError:  
>> empty string for float()"
>> "
>> here's the code
>>
>> ************
>>
>> import sys
>> import re
>>
>> for line in sys.stdin.readlines():
>> 	my_line = line.rstrip()
>> 	numb = float(my_line)
>> 	print numb
>>   
>>     
> The clue "empty string for float" must mean that the value of my_line is
> an empty string.  And that would seem to mean you have an empty line in
> your input. 
>
> So...  Either remove the empty lines, or test my_line before calling
> float on it.
>
> Gary Herron
>
>   
Or let the exception get raised, and handle it.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070830/3dbb0a8a/attachment.html>


More information about the Python-list mailing list