isdigit( ) not supported in PythonWin?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yueying53
    New Member
    • Dec 2008
    • 13

    isdigit( ) not supported in PythonWin?

    Hi people!

    I'm working with a Telit gsm module and am using PythonWin. I am trying to use the function isdigit( ) to check if a string contains only digits. However, PythonWin doesn't seem to support this function.

    Does anyone know what I can do to work around this problem? I really need this function and cannot find other alternatives...

    Thanks in advance!

    Cheers, Samantha
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Samantha,

    isdigit() is a string method and is available in PythonWin.
    Code:
    >>> s = '1234567890'
    >>> s.isdigit()
    True
    >>> s1 = '1234567890a'
    >>> s1.isdigit()
    False
    >>>
    -BV

    Comment

    • yueying53
      New Member
      • Dec 2008
      • 13

      #3
      Hi BV,

      Am I supposed to import any particular module to be able to use isdigit()? Because Telit's Easy Script in Python document suggests that this function is not supported. Also, I checked the library for string and isdigit() is not in there either.

      I was thinking if I could write the function manually myself and put it in the string module. But how should this function be written?

      Thanks a million

      Cheers, Samantha

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Samantha,

        isdigit() is a string method and is built into Python (I am using 2.3). Following are a couple of functions that return True of all the characters in a string are digits and False if not.
        [code=Python]def isdigit(s):
        for c in s:
        if not (48 <= ord(c) <= 57):
        return False
        return True

        print isdigit('123456 789a')
        print isdigit('123456 7890')
        print isdigit('Z1994. dhh5')

        print

        def isdigit(s):
        try:
        int(s)
        except:
        return False
        return True[/code]
        Example usage:
        [code=Python]>>> isdigit('123456 789a')
        False
        >>> isdigit('123456 7890')
        True
        >>> [/code]

        Comment

        • yueying53
          New Member
          • Dec 2008
          • 13

          #5
          BV,

          Thank you so much for your help. I'm quite new to Python so pardon me for being unable to distinguish between string 'methods' and functions. Thank you for your corrections.

          Anyhows, I do realise that isdigit( ) is supposed to be inbuilt into Python. That's why I was surprised that an error returned when I ran that part of the code. Not sure why that happens to the Python that I'm using. But I got around the problem by manually writing the function that you provided above. It works as expected now.

          Thanks a million once again! =)

          Cheers, Samantha

          Comment

          • Smygis
            New Member
            • Jun 2007
            • 126

            #6
            When you start python you will get a message containing youre python version. Like this:
            Code:
            Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
            [GCC 4.3.2] on linux2
            Type "help", "copyright", "credits" or "license" for more information.
            >>> >>> dir("")
            ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', [B]'isdigit',[/B] 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

            Comment

            Working...