1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python > codeがprintableかどうかのチェック > ord()使用 / all(c in string.printable for c in hello)

Last updated at Posted at 2016-02-16

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
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?