I'm trying to validate a set of strings to report out the usage of illegal ANSI characters. I've read that extended ASCII is NOT exactly similar to ANSI. I've been trying to search for a way to check if a character is an ANSI character, but so far I found none. Does anyone know how to do this in Python?
Asked
Active
Viewed 407 times
1
-
https://stackoverflow.com/questions/701882/what-is-ansi-format suggests "ansi" is not enough to specify an encoding. Which specific ANSI encoding do you wish to validate? – Paul Hankin Jul 28 '21 at 07:58
-
The other question is what exactly you mean by illegal characters. – Paul Hankin Jul 28 '21 at 08:01
2 Answers
2
Try with ord(c) function:
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"

rnso
- 23,686
- 25
- 112
- 234
1
This might help you with detecting any ANSI character in a text :
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()
Found this code on this question.

Jules Civel
- 449
- 2
- 13