#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    180
    Rep Power
    11

    Testing for non-printable characters


    Hi,

    I am trying to figure out how to test if there are non-printable characters in a given string.

    For example,

    givenString = 'test characters'
    for c in givenString:
    if (c == .....):
    return "not a valid string"

    So in my if(c == ...) I am not sure what to replace the ..... with.

    Any ideas?

    -Laura
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    The string module has printable, which contains a list of printable characters.
    Code:
    import string
    
    if c in string.printable:
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    180
    Rep Power
    11
    It is not working. This is what I have

    Code:
     
    import string
    passwd1 = 'string 123'
    check = 0
    
    for c in passwd1:
     if c in string.printable:
      check = check + 1
     else:
      print "Password cannot contain non-printable chracters"
    
    print check
    check returns 10 which means it is not triggering the error message for the space in between string and 123. I tried putting a tab in between the string also and it didn't work.

    Am I forgetting something?
    -Laura
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    This is because string.printable considers whitespace as printable characters as well.
    http://docs.python.org/lib/module-string.html
    If you want to exclude whitespace, you can check if c is in string.letters, string.punctuation and string.digits.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    180
    Rep Power
    11
    Ok. I think I should have used string.whitespace

    This is at least a start for me. But, in actuality I need to put more restrictions on my string. For example, it must contain at least 1 special character (! @ # $ & ^) but it cannot contain (\ / |). Plus, the string must have at least one digit along with no whitespace characters.

    I know that I may need to create a regular expression but I am not that confident on how to build it with all of these restrictions.

    Thanks for you help.

    -Laura
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    If you deside to give the regex ago (and i think its really you're best bet here,) then you might want to check out Kodos: the Python regular expression debugger. I love this thing, it just makes working with regex and Python fun.

    http://kodos.sourceforge.net/

    Note: Since Python and perl use the same regex you could always use this to test regex for perl too. Alternativly, if you can find a regex that already does what you want in perl. You should be able to use that .

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    You could also take the easy way out and perform several checks on the string as you go and then check flags at the end to see if the string passes your requirements.
    Code:
    #!/usr/bin/env python                 
    import string
    
    def checkstr(param):
        specialcount = 0
        bad = 0
        goodsyms = ['!', '@', '#', '$', '&', '^']
        for c in param:
            if not(c in string.printable):
                bad = 1
                break
    
            if c in string.punctuation:
                if c in goodsyms:
                    specialcount += 1
                else:
                    bad = 1
                    break
    
        if bad == 1 or specialcount != 1:
            return 0
        else:
            return 1
    
    
    teststr = "This is a big string!"
    print checkstr(teststr)
    teststr = "This is a big string with no punctuation"
    print checkstr(teststr)
    teststr = "This is a big string with extra punctuation!!$"
    print checkstr(teststr)
    teststr = "This is a big string with bad punctuation/|?"
    print checkstr(teststr)
    This approach doesn't use regexs and is pretty simple to understand as well
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Posts
    180
    Rep Power
    11
    Yes, that is alot easier.

    Thanks for your help
    Laura

IMN logo majestic logo threadwatch logo seochat tools logo