Skip to content

Instantly share code, notes, and snippets.

@dmitriiweb
Last active July 25, 2020 12:38
Show Gist options
  • Select an option

  • Save dmitriiweb/641936a94fa51a0069464854c5f4c031 to your computer and use it in GitHub Desktop.

Select an option

Save dmitriiweb/641936a94fa51a0069464854c5f4c031 to your computer and use it in GitHub Desktop.
Decorator, remove non visible characters from string
def clear_text(func):
"""Remove non visible characters (\\n, \\r, \\t)
Example:
>>> @clear_text
>>> def foo():
... return "blah \\n blah"
...
>>> foo()
... blah blah
:param func: function for decoration
"""
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return clean_txt(res)
return wrapper
def clean_txt(text: str) -> str:
"""Remove non visible characters (\\n, \\r, \\t)
:param str text: some text
:return: cleaned text
"""
trans = {
"\r": "",
"\n": "",
"\t": "",
}
try:
text = text.translate(str.maketrans(trans))
except AttributeError:
pass
return text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment