[Tutor] Help with checking for a data type

christopher.henk at allisontransmission.com christopher.henk at allisontransmission.com
Thu Sep 4 23:21:12 CEST 2008


try and convert the first value in the split list to a int (or float), if 
that works then post the value.
if it throws an exception (ValueError) then catch that and pass, or do 
whatever else needs doing with the listing.

def postData(inputData):
    fields=inputData.split("-")
    try:
        numberfield=int(fields[0])
        #do whatever voodoo needed
        print "%s posted" %inputData
    except ValueError:
        print "%s not posted" %inputData
        pass
    #move on to other fields
 
inputValue=["12-Foo-Fee","Bla-Ble-blu"]

for entry in inputValue:
    postData(entry)




"Spencer Parker" <inthefridge at gmail.com> 
Sent by: tutor-bounces+christopher.henk=allisontransmission.com at python.org
09/04/2008 04:56 PM

To
tutor at python.org
cc

Subject
[Tutor] Help with checking for a data type






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?

-- 
Spencer Parker
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080904/96b71699/attachment-0001.htm>


More information about the Tutor mailing list