I want to check whether the RS-232C communication code is a control character such as <ACK>
or a printable one such as G
.
Reference http://python.civic-apps.com/char-ord/
I implemented the following.
http://ideone.com/XKsbnY
checkCode.py
def checkCode(code):
if ord(code) < 32:
print "not printable"
if ord(code) >= 32:
print "printable:", code
code = chr(6) # <ACK>
checkCode(code)
code = chr(71) # G
checkCode(code)
result
Success time: 0.01 memory: 8968 signal:0
not printable
printable: G
http://stackoverflow.com/questions/3636928/test-if-a-python-string-is-printable According to it, there is also the following writing style.
>>> import string
>>> all(c in string.printable for c in hello)
True
Recommended Posts