A question about string and float number

Wei Guo weiguo6 at gmail.com
Thu Aug 7 16:28:46 EDT 2008


Hi I tried the first type method but it seens that it doesn't work. Could
anyone help me about it?

>>> s = '3.145'
>>> type(s) == type(float())
False
>>> type(s)
<type 'str'>
>>> type(float())
<type 'float'>
>>>

Best regards,

Wei


On 8/7/08, Wei Guo <weiguo6 at gmail.com> wrote:
>
> Hi Thanks for Tyler and Edwin's help.
>
> For my questions, I need to import some xml file and there are floating
> number and strings in it. I need to process string and number differently.
> This is reason that I am asking question here. Is this background
> information we need for this quesions.
>
> Btw, which way is better? type or with exception ValueError?
>
> Thanks,
>
> Wei
>
>
>  On 8/6/08, Tyler Breisacher <dancinguy at linkline.com> wrote:
>>
>> It's generally a bad idea to use "except" without naming a specific
>> exception. The exception you might expect in this case is ValueError. Any
>> other exception *should* be uncaught if it happens. By the way, this method
>> will return true for integers as well as floats. For example, isFloat('3')
>> will return 3.0. So make sure this is what you want, since it wasn't 100%
>> clear from the original message.
>>
>>
>> Wei Guo wrote:
>>
>>> #this is a better way of testing a string for float
>>> def isFloat(s):
>>>    try:
>>>        s = float(s)
>>>    except:
>>>        return False
>>>    return True
>>>
>>
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080807/85ebc499/attachment-0001.html>


More information about the Python-list mailing list