Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions code_to_optimize/remove_control_chars.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import re


class CharacterRemover:
def __init__(self):
self.version = "0.1"
self._translation_table = dict.fromkeys(
range(32)
) # Create a translation table removing control characters 0x00-0x1F
self._translation_table.update({127: None}) # Also remove 0x7F (DEL)

def remove_control_characters(self, s) -> str:
"""Remove control characters from the string."""
return re.sub("[\\x00-\\x1F\\x7F]", "", s) if s else ""
return s.translate(self._translation_table) if s else ""
Loading