Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 13th, 2004, 11:53 AM
lauramccord lauramccord is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 180 lauramccord User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 3 m 58 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old October 13th, 2004, 12:43 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,672 Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 23 h 13 m 55 sec
Reputation Power: 1083
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

Reply With Quote
  #3  
Old October 13th, 2004, 12:59 PM
lauramccord lauramccord is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 180 lauramccord User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 3 m 58 sec
Reputation Power: 5
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

Reply With Quote
  #4  
Old October 13th, 2004, 01:05 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,672 Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 23 h 13 m 55 sec
Reputation Power: 1083
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.

Reply With Quote
  #5  
Old October 13th, 2004, 01:08 PM
lauramccord lauramccord is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 180 lauramccord User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 3 m 58 sec
Reputation Power: 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

Reply With Quote
  #6  
Old October 13th, 2004, 01:44 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 10 m 32 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #7  
Old October 13th, 2004, 01:57 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,672 Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Days 23 h 13 m 55 sec
Reputation Power: 1083
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

Reply With Quote
  #8  
Old October 14th, 2004, 01:05 PM
lauramccord lauramccord is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 180 lauramccord User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 3 m 58 sec
Reputation Power: 5
Yes, that is alot easier.

Thanks for your help
Laura

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Testing for non-printable characters


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT