To strip non-printable characters from a string in Python, you can use a regular expression along with the re module. Here's an example:
import re def strip_non_printable(input_string): # Use a regular expression to remove non-printable characters printable_regex = re.compile('[^ -~]+') stripped_string = printable_regex.sub('', input_string) return stripped_string # Example usage: original_string = "Hello, world!\x01\x02\x03" stripped_string = strip_non_printable(original_string) print("Original string:", repr(original_string)) print("Stripped string:", repr(stripped_string)) In this example:
[^ -~]+ matches any character that is not in the printable ASCII range (32 to 126).re.sub function is used to replace those non-printable characters with an empty string.Note that the printable ASCII range (-~) includes characters from space (' ') to tilde ('~'), covering most of the common printable characters.
Adjust the regular expression or modify the code based on your specific requirements and the definition of "non-printable" characters in your context.
Remove non-printable characters using a regex in Python:
# Code Implementation import re cleaned_string = re.sub(r'[^\x20-\x7E]', '', input_string)
Strip non-printable characters using isprintable() method in Python:
isprintable() method to filter out non-printable characters from a string.# Code Implementation cleaned_string = ''.join(char for char in input_string if char.isprintable())
Remove non-printable characters using ASCII range in Python:
# Code Implementation cleaned_string = ''.join(char for char in input_string if 32 <= ord(char) <= 126)
Strip control characters using string.printable in Python:
string.printable constant to remove non-printable characters from a string.# Code Implementation import string cleaned_string = ''.join(char for char in input_string if char in string.printable)
Remove non-printable characters using a list comprehension in Python:
# Code Implementation cleaned_string = ''.join([char for char in input_string if 32 <= ord(char) <= 126])
Strip non-printable characters using isascii() method in Python 3.7+:
isascii() method to remove non-ASCII characters, which includes non-printable ones.# Code Implementation cleaned_string = ''.join(char for char in input_string if char.isascii())
Remove non-printable characters using filter and lambda in Python:
filter function with a lambda function to remove non-printable characters.# Code Implementation cleaned_string = ''.join(filter(lambda char: 32 <= ord(char) <= 126, input_string))
Strip non-printable characters using unicodedata.category in Python:
unicodedata.category function to filter out non-printable characters.# Code Implementation import unicodedata cleaned_string = ''.join(char for char in input_string if unicodedata.category(char)[0] != 'C')
Remove non-printable characters using re.sub with Unicode in Python:
re.sub with Unicode character classes to strip non-printable characters.# Code Implementation cleaned_string = re.sub(r'[\x00-\x1F\x7F-\x9F]', '', input_string)
Strip non-printable characters using string.whitespace in Python:
string.whitespace constant to preserve whitespace characters.# Code Implementation import string cleaned_string = ''.join(char for char in input_string if char in string.whitespace or 32 <= ord(char) <= 126)
intel-edison read.table openpgp google-polyline architecture shadow-dom stub quickblox testbed azure-rm-template