By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,497 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,497 IT Pros & Developers. It's quick & easy.

How to detect ascii or non-ascii strings...

P: 17
Hi friends,
can anybody tell me how to detect ascii or non-ascii strings using python.
Jan 21 '08 #1
Share this Question
Share on Google+
1 Reply


P: 56
Theres probably a more elegant way, or even, a built in function, but here it goes. This checks an entire string if every character is in the standard ascii tablfor the extended ascii table, you will need to extend the range of the "ascii" list.

Expand|Select|Wrap|Line Numbers
  1. >>> ascii = [chr(c) for c in range(128)]
  2. >>> def is_ascii(string):
  3. ...     for character in string:
  4. ...             if not character in ascii: return False
  5. ...     return True
  6. ...
  7. >>> is_ascii("test")
  8. True
  9. >>> is_ascii("test\x80")
  10. False
  11. >>>
Jan 22 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.