我在找这样的东西:
allBytes = b'\1Hello World!\127\0'
print(countNonPrintables(allBytes)) # prints 3
您可以迭代所有字节并手动确定它们的可打印性,但这对我来说太慢了,尤其是对于字节对象>;2k字节
(慢)例如:
def countNonPrintables(allBytes: bytes) -> int:
nonPrintablesCount = 0
for b in allBytes:
# DEL:
if b == b'\127':
nonPrintablesCount += 1
continue
if ord(b) < 32:
# BS, HT, ESC, FF:
if b not in {b'\b', b'\t', b'\033', b'\014'}:
nonPrintablesCount += 1
return nonPrintablesCount
我不确定您决定的内容是否可打印,但您可以使用python字符串方法
isprintable
因此,您可以在每个字符上循环并调用isprintable方法
输出
相关问题 更多 >
编程相关推荐