Skip to content

hansalemaos/get_the_hell_out_of_here

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A function to remove control characters / non-printing characters from strings and binaries
$pip install get-the-hell-out-of-here
from get_the_hell_out_of_here import remove_escaped_characters
import numpy as np
test1 = np.array(["hi, my friend", "are you fine?"]).tobytes() #lets create some test data

#numpy as bytes
b"h\x00\x00\x00i\x00\x00\x00,\x00\x00\x00 \x00\x00\x00m\x00\x00\x00y\x00\x00\x00 \x00\x00\x00f\x00\x00\x00r\x00\x00\x00i\x00\x00\x00e\x00\x00\x00n\x00\x00\x00d\x00\x00\x00a\x00\x00\x00r\x00\x00\x00e\x00\x00\x00 \x00\x00\x00y\x00\x00\x00o\x00\x00\x00u\x00\x00\x00 \x00\x00\x00f\x00\x00\x00i\x00\x00\x00n\x00\x00\x00e\x00\x00\x00?\x00\x00\x00"
print(remove_escaped_characters(test1))
#Result:  b'hi, my friendare you fine?'

test2 = b"h\\x00\\x00\\x00i\\x00\\x00\\x00,\\x00\\x00\\x00 \\x00\\x00\\x00m\\x00\\x00\\x00y\\x00\\x00\\x00 \\x00\\x00\\x00f\\x00\\x00\\x00r\\x00\\x00\\x00i\\x00\\x00\\x00e\\x00\\x00\\x00n\\x00\\x00\\x00d\\x00\\x00\\x00a\\x00\\x00\\x00r\\x00\\x00\\x00e\\x00\\x00\\x00 \\x00\\x00\\x00y\\x00\\x00\\x00o\\x00\\x00\\x00u\\x00\\x00\\x00 \\x00\\x00\\x00f\\x00\\x00\\x00i\\x00\\x00\\x00n\\x00\\x00\\x00e\\x00\\x00\\x00?\\x00\\x00\\x00" #double escaped
print(remove_escaped_characters(test2))
# Result:   b'hi, my friendare you fine?'


test3 = "h\x00\x00\x00i\x00\x00\x00,\x00\x00\x00 \x00\x00\x00m\x00\x00\x00y\x00\x00\x00 \x00\x00\x00f\x00\x00\x00r\x00\x00\x00i\x00\x00\x00e\x00\x00\x00n\x00\x00\x00d\x00\x00\x00a\x00\x00\x00r\x00\x00\x00e\x00\x00\x00 \x00\x00\x00y\x00\x00\x00o\x00\x00\x00u\x00\x00\x00 \x00\x00\x00f\x00\x00\x00i\x00\x00\x00n\x00\x00\x00e\x00\x00\x00?\x00\x00\x00" #as string
print(remove_escaped_characters(test3))
#Result: hi, my friendare you fine?

test4 = "h\\x00\\x00\\x00i\\x00\\x00\\x00,\\x00\\x00\\x00 \\x00\\x00\\x00m\\x00\\x00\\x00y\\x00\\x00\\x00 \\x00\\x00\\x00f\\x00\\x00\\x00r\\x00\\x00\\x00i\\x00\\x00\\x00e\\x00\\x00\\x00n\\x00\\x00\\x00d\\x00\\x00\\x00a\\x00\\x00\\x00r\\x00\\x00\\x00e\\x00\\x00\\x00 \\x00\\x00\\x00y\\x00\\x00\\x00o\\x00\\x00\\x00u\\x00\\x00\\x00 \\x00\\x00\\x00f\\x00\\x00\\x00i\\x00\\x00\\x00n\\x00\\x00\\x00e\\x00\\x00\\x00?\\x00\\x00\\x00"  #as string - double escaped
print(remove_escaped_characters(test4))
#Result: hi, my friendare you fine?