Reputation: 85
I am reading in a file using binary settings:
with open(filename, 'rb') as f:
I am then reading the entire file into a variable:
x = f.read()
My problem is that I want to check if the bytes in x are ascii printable. So i want to compare the bytes to see if they are within the range of say 32-128 in decimal notation. What would be the easiest way to go about doing this?
I have toyed around with the ord() function, various hex functions since I have previously converted the bytes into hex elsewhere in my project, but nothing seems to be working.
I'm new to python but have experience in other languages. Can anyone point me in the right direction? Thanks.
Upvotes: 4
Views: 8615
Reputation: 3599
i want to compare the bytes to see if they are within the range of say 32-128 in decimal notation
just do it ; )
byte_str = b"".join(([ bytes(chr(i), 'ascii') for i in range(128) ]))
for byte in byte_str:
if byte < 32 or 128 < byte:
print(f"byte_str is not printable because of byte {repr(chr(byte))}")
break
example result
byte_str is not printable because of byte '\x00'
Upvotes: 0
Reputation: 87124
You could check each byte against string.printable
.
>>> import string
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
printable_chars = bytes(string.printable, 'ascii')
with open(filename, 'rb') as f:
printable = all(char in printable_chars for char in f.read())
For greater efficiency, O(1) vs O(n) for the set vs string lookup, use a set:
printable_chars = set(bytes(string.printable, 'ascii'))
with open(filename, 'rb') as f:
printable = all(char in printable_chars for char in f.read())
Upvotes: 4