除了具有转义序列的字符外,我们如何将ASCII字符列表打印到控制台



对于任何ascii字符ch,我都希望打印ch,除非repr(ch)以反斜杠字符开头。

我想打印所有">nice";ascii字符。

失败的尝试

import re

characters = [chr(num) for num in range(256)]
# `characters` : a list such that every element
#                of the list is an ASCII character
escaped_chars = [repr(ch)[1:-1] for ch in characters]
# `escaped_chars`: a list of all ASCII character unless
#               the character is special
#               new line is stored as "n"
#               carriage return is stored as "r"
printables = "".join(filter(lambda s: s[0] != "\", escaped_chars))
print("n".join(re.findall('.{1,20}', "".join(printables))))

控制台打印输出为:

!"#$%&'()*+,-./0123
456789:;<=>?@ABCDEFG
HIJKLMNOPQRSTUVWXYZ[
]^_`abcdefghijklmnop
qrstuvwxyz{|}~¡¢£¤¥¦
§¨©ª«¬®¯°±²³´µ¶·¸¹º»
¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ
ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâã
äåæçèéêëìíîïðñòóôõö÷
øùúûüýþÿ

我似乎打印了很多奇怪的unicode字符,比如çõ

import re
characters = [chr(num) for num in range(127)]
# `characters` : a list such that every element
#                of the list is an ASCII character
escaped_chars = [repr(ch)[1:-1] for ch in characters]
# `escaped_chars`: a list of all ASCII character unless
#               the character is special
#               new line is stored as "n"
#               carriage return is stored as "r"
printables = "".join(filter(lambda s: s[0] != "\", escaped_chars))
print("n".join(re.findall('.{1,20}', "".join(printables))))

正如sj95126所写:

超过127的值不是ASCII字符

这是正确的代码

最新更新