[Tutor] Help with checking for a data type

Spencer Parker inthefridge at gmail.com
Thu Sep 4 23:30:21 CEST 2008


Those worked for me...thanks again!!!

On Thu, Sep 4, 2008 at 3:26 PM, Kent Johnson <kent37 at tds.net> wrote:

> On Thu, Sep 4, 2008 at 4:56 PM, Spencer Parker <inthefridge at gmail.com>
> wrote:
> > I have a script that is taking a directory list, the script then splits
> the
> > name up by the hyphens in the name.  The first part of the name should be
> a
> > number, but it isn't always a number.  Is there a way to say: if its a
> > number then post this data....if not discard?
>
> Tthe isdigit() method of a string provides a simple test. It will
> return true if each character is a digit.
> if first_part.isdigit():
>  # post data
>
> If you want to allow things like '-300' or '1.234' then you can try to
> convert to an int or float. Failure will raise a ValueError.
> try:
>  float(first_part)
>  # post data
> except ValueError:
>  pass
>
> Kent
>



-- 
Spencer Parker
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080904/36a41447/attachment.htm>


More information about the Tutor mailing list