问题:在打印ascii码表时有一些不可打印的部分,希望各位大神教教我怎么替换掉,我用了replace结果替换不了,不太明白怎么把不可打印的部分处理成✳️。图如下标注:
通过搜索的资料,发现可以使用isprintable()来判断字符串中的字符是否可以被打印。
代码如下:
参考链接:
for i in range (0,256):
tplt = "{0:^10}:{1:^10}:{2:^10} "
# https://cloud.tencent.com/developer/article/2173470
# https://www.bbsmax.com/A/obzbLPay5E/
# https://blog.csdn.net/zengcong2013/article/details/18702945
# https://blog.csdn.net/LaoYuanPython/article/details/94211925
# 使用isprintable方法判断字符串中的字符是否可以打印,如果可以则直接,否则输出为星号
if str(chr(i)).isprintable()== True:
print(tplt.format(i,hex(i),chr(i),sep=":",end=" "))
else :
print(tplt.format(i,hex(i),'*',sep=":",end=" "))
在Python中,我们经常需要进行数字和ASCII字符进行互换,常见字符的ASCII如下所示:
48-57 数字0-9
97-122 小写字母a-z
用Python实现数字转ASCII码的代码如下:
print(chr(65))
print(chr(115))
print(chr(99))
print(chr(105))
print(chr(105))
输出如下:
A
s
c
i
i
反之,我们可以使用ord函数,进行ascii码转成对应的数字,代码如下:
num=ord('a')
print(num)
输出如下:
97