10 examples of 'remove all special characters from string python' in Python

Every line of 'remove all special characters from string python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
42def remove(value, delete_chars):
43 for c in delete_chars:
44 value = value.replace(c, '')
45 return value
448def remove_control_characters(s):
449 if s is None:
450 return ''
451
452 s = unicode(s, "utf-8")
453 return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")
78def clean_string(string):
79 string = re.sub('@', '@\u200b', string)
80 string = re.sub('#', '#\u200b', string)
81 string = re.sub('`', '', string)
82 return string
86def stripIllegalChars(str):
87
88 # If the char separates a word, e.g. Face/Off, we need to preserve that separation with a -
89 str = re.sub(r'(?<=\S)[' + patterns.illegalChars + r'](?=\S)', '-', str)
90
91 # If it terminates another word, e.g. Mission: Impossible, we replace it, and any surrounding spaces with a single space
92 str = re.sub(r'\s?[' + patterns.illegalChars + r']\s?', ' ', str)
93
94 return str
76def _clean_win_chars(string):
77 """Windows cannot encode some characters in filename."""
78 import urllib
79 if hasattr(urllib, 'quote'):
80 quote = urllib.quote
81 else:
82 # In Python 3, quote is elsewhere
83 import urllib.parse
84 quote = urllib.parse.quote
85 for char in ('<', '>', '!', ':', '\\'):
86 string = string.replace(char, quote(char))
87 return string
34def replace_escape_char(a_string):
35
36 return _replace_by_regex(a_string, r'\[\s|\n]*')
202def remove_newlines(string):
203 string = string.replace("\n", "")
204 return string.replace("\r", "")
158def remove_newlines(value):
159 if not value:
160 return ''
161 elif isinstance(value, six.string_types):
162 return value.replace('\r', '').replace('\n', '')
163 else:
164 return value
139def remove_spaces(string):
140 "Elimina los espacios innecesarios de una fila de tabla."
141 return re.sub("\s\s+", " ", string)
74def removeWhitespaceChars(s):
75 """
76 Removes whitespace characters
77 @ In, s, string, to remove characters from
78 @ Out, s, string, removed whitespace string
79 """
80 s = s.replace(" ","")
81 s = s.replace("\t","")
82 s = s.replace("\n","")
83 #if this were python3 this would work:
84 #removeWhitespaceTrans = "".maketrans("",""," \t\n")
85 #s = s.translate(removeWhitespaceTrans)
86 return s

Related snippets