RS-232C通信のコードが<ACK>
などのコントロールキャラクタであるか、G
などのprintableなものであるかの確認をしたい。
参考 http://python.civic-apps.com/char-ord/
以下を実装してみた。
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)
結果
Success time: 0.01 memory: 8968 signal:0
not printable
printable: G
http://stackoverflow.com/questions/3636928/test-if-a-python-string-is-printable
によると以下のような書き方もあるらしい。
>>> import string
>>> all(c in string.printable for c in hello)
True