Quantcast
Channel: Howtos & more …– LANbugs
Viewing all articles
Browse latest Browse all 144

Python: Entfernen von Steuerzeichen in ASCII Strings

$
0
0

Bin heute wieder mal über ein Problem mit ASCII Strings gestolpert. Im String war ein CTRL+C welches als \x03 in der ASCII Tabelle ist. Ich bin auf eine Seite gestoßen welche für alle möglichen Script und Programmiersprachen Codesnippets bereitstellt um Steuerzeichen zu entfernen.

ASCII Tabelle bei Wikipedia: https://de.wikipedia.org/wiki/American_Standard_Code_for_Information_Interchange

Hier für Python:

stripped = lambda s: "".join(i for i in s if 31 < ord(i) < 127)
 
print(stripped("\ba\x00b\n\rc\fd\xc3"))

Output:

abcd

Quelle: https://rosettacode.org/wiki/Strip_control_codes_and_extended_characters_from_a_string

Alternativ kann man auch einzelne ASCII Codes ersetzen mit der Regex Library

Beispiel:

import re

data = "ab\x03cd"
data_clean = re.sub(r"\x03","",data)

print data

abcd

Viel Spaß 🙂


Viewing all articles
Browse latest Browse all 144