Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check that a string contains only ASCII characters?

How do I check that a string only contains ASCII characters in Python? Something like Ruby's ascii_only?

I want to be able to tell whether string specific data read from file is in ascii

like image 546
JavaSa Avatar asked Mar 09 '16 10:03

JavaSa


1 Answers

In Python 3.7 were added methods which do what you want:

str, bytes, and bytearray gained support for the new isascii() method, which can be used to test if a string or bytes contain only the ASCII characters.


Otherwise:

>>> all(ord(char) < 128 for char in 'string')
True
>>> all(ord(char) < 128 for char in 'строка')
False

Another version:

>>> def is_ascii(text):
    if isinstance(text, unicode):
        try:
            text.encode('ascii')
        except UnicodeEncodeError:
            return False
    else:
        try:
            text.decode('ascii')
        except UnicodeDecodeError:
            return False
    return True
... 
>>> is_ascii('text')
True
>>> is_ascii(u'text')
True
>>> is_ascii(u'text-строка')
False
>>> is_ascii('text-строка')
False
>>> is_ascii(u'text-строка'.encode('utf-8'))
False
like image 136
warvariuc Avatar answered Oct 16 '22 14:10

warvariuc