-1

I get bytecode data (b'something') which I try: to .decode('ascii') to check if this is ASCII text. The problem is that

In [11]: b'\x00\x0c\x00'.decode('ascii')
Out[11]: u'\x00\x0c\x00'

so what is recognized as "text" is not really what I want (which are 32 to 126 ASCII codes). Is there a way to use a subset of 'ascii' for the decoding?

WoJ
  • 27,165
  • 48
  • 180
  • 345

1 Answers1

1

in python 2:

def test_if_ascii(text):
    if isinstance(test, unicode):
        raise TypeError('hey man, dont feed me unicode plz')
    return all(32 <= ord(c) <= 126 for c in text)

in python 3 almost the same, just unicode is call 'str', and bytes are called 'bytes'

def test_if_ascii(text):
    if isinstance(test, str):
        raise TypeError('hey man, dont feed me unicode plz')
    return all(32 <= ord(c) <= 126 for c in text)
Stephane Martin
  • 1,612
  • 1
  • 17
  • 25