[英]How to colour a specific character cell in python using ansi escape codes where the character cell location is determined by variables
[英]How to check for ANSI character in Python
我正在嘗試驗證一組字符串以報告非法 ANSI 字符的使用情況。 我讀過擴展 ASCII與ANSI不完全相似。 我一直在尋找一種方法來檢查一個字符是否是 ANSI 字符,但到目前為止我沒有找到。 有誰知道如何在 Python 中做到這一點?
這可能會幫助您檢測文本中的任何 ANSI 字符:
split_ANSI_escape_sequences = re.compile(r"""
(?P<col>(\x1b # literal ESC
\[ # literal [
[;\d]* # zero or more digits or semicolons
[A-Za-z] # a letter
)*)
(?P<name>.*)
""", re.VERBOSE).match
def split_ANSI(s):
return split_ANSI_escape_sequences(s).groupdict()
在這個問題上找到了這個代碼。
嘗試使用 ord(c) 函數:
def detect_non_printable(s):
for c in s:
n = ord(c)
if n < 32 or n > 126:
return "NON-PRINTABLE DETECTED"
return "PRINTABLE CHARS ONLY"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.