|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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 Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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 ![]() |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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 |
|
#8
|
|||
|
|||
|
Yes, that is alot easier.
Thanks for your help Laura |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Testing for non-printable characters |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|