<div>Hi Grant,</div>
<div> </div>
<div>I am using the try/exception and just curious why function type doesn't work. </div>
<div> </div>
<div>Best regards,</div>
<div> </div>
<div>Wei<br><br> </div>
<div><span class="gmail_quote">On 8/7/08, <b class="gmail_sendername">Timothy Grant</b> <<a href="mailto:timothy.grant@gmail.com">timothy.grant@gmail.com</a>> wrote:</span>
<blockquote class="gmail_quote" style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">That's because s IS a string. It's not been converted to a float.<br><br>In [1]: s = '3.1415'<br>
In [2]: n = float(s)<br>In [3]: type(s)<br>Out[3]: <type 'str'><br>In [4]: type(n)<br>Out[4]: <type 'float'><br><br>Why are you avoiding the very simple try:/except: solution to this problem?<br>
<br>On Thu, Aug 7, 2008 at 1:28 PM, Wei Guo <<a href="mailto:weiguo6@gmail.com">weiguo6@gmail.com</a>> wrote:<br>> Hi I tried the first type method but it seens that it doesn't work. Could<br>> anyone help me about it?<br>
><br>>>>> s = '3.145'<br>>>>> type(s) == type(float())<br>> False<br>>>>> type(s)<br>> <type 'str'><br>>>>> type(float())<br>> <type 'float'><br>
>>>><br>><br>> Best regards,<br>><br>> Wei<br>><br>><br>> On 8/7/08, Wei Guo <<a href="mailto:weiguo6@gmail.com">weiguo6@gmail.com</a>> wrote:<br>>><br>>> Hi Thanks for Tyler and Edwin's help.<br>
>><br>>> For my questions, I need to import some xml file and there are floating<br>>> number and strings in it. I need to process string and number differently.<br>>> This is reason that I am asking question here. Is this background<br>
>> information we need for this quesions.<br>>><br>>> Btw, which way is better? type or with exception ValueError?<br>>><br>>> Thanks,<br>>><br>>> Wei<br>>><br>>><br>>> On 8/6/08, Tyler Breisacher <<a href="mailto:dancinguy@linkline.com">dancinguy@linkline.com</a>> wrote:<br>
>>><br>>>> It's generally a bad idea to use "except" without naming a specific<br>>>> exception. The exception you might expect in this case is ValueError. Any<br>>>> other exception *should* be uncaught if it happens. By the way, this method<br>
>>> will return true for integers as well as floats. For example, isFloat('3')<br>>>> will return 3.0. So make sure this is what you want, since it wasn't 100%<br>>>> clear from the original message.<br>
>>><br>>>><br>>>> Wei Guo wrote:<br>>>>><br>>>>> #this is a better way of testing a string for float<br>>>>> def isFloat(s):<br>>>>>    try:<br>>>>>        s = float(s)<br>
>>>>    except:<br>>>>>        return False<br>>>>>    return True<br>>>><br>>>><br>>>> --<br>>>> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>
>><br>><br>><br>> --<br>> <a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br>><br><br><br><br>--<br>Stand Fast,<br>tjg.  [Timothy Grant]<br>
</blockquote></div><br>