Stripping non printable characters from a string in python

Stripping non printable characters from a string in python

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:

  • The regular expression [^ -~]+ matches any character that is not in the printable ASCII range (32 to 126).
  • The 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.

Examples

  1. Remove non-printable characters using a regex in Python:

    • "Python remove non-printable characters regex"
    • Description: Use a regular expression to strip non-printable characters from a string.
    # Code Implementation import re cleaned_string = re.sub(r'[^\x20-\x7E]', '', input_string) 
  2. Strip non-printable characters using isprintable() method in Python:

    • "Python strip non-printable characters isprintable"
    • Description: Utilize the 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()) 
  3. Remove non-printable characters using ASCII range in Python:

    • "Python remove non-printable characters ASCII range"
    • Description: Filter characters based on their ASCII values to strip non-printable characters.
    # Code Implementation cleaned_string = ''.join(char for char in input_string if 32 <= ord(char) <= 126) 
  4. Strip control characters using string.printable in Python:

    • "Python strip control characters string.printable"
    • Description: Utilize the 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) 
  5. Remove non-printable characters using a list comprehension in Python:

    • "Python remove non-printable characters list comprehension"
    • Description: Use a list comprehension to filter out non-printable characters.
    # Code Implementation cleaned_string = ''.join([char for char in input_string if 32 <= ord(char) <= 126]) 
  6. Strip non-printable characters using isascii() method in Python 3.7+:

    • "Python strip non-printable characters isascii"
    • Description: Use the 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()) 
  7. Remove non-printable characters using filter and lambda in Python:

    • "Python remove non-printable characters filter lambda"
    • Description: Apply the 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)) 
  8. Strip non-printable characters using unicodedata.category in Python:

    • "Python strip non-printable characters unicodedata.category"
    • Description: Utilize the 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') 
  9. Remove non-printable characters using re.sub with Unicode in Python:

    • "Python remove non-printable characters re.sub Unicode"
    • Description: Use re.sub with Unicode character classes to strip non-printable characters.
    # Code Implementation cleaned_string = re.sub(r'[\x00-\x1F\x7F-\x9F]', '', input_string) 
  10. Strip non-printable characters using string.whitespace in Python:

    • "Python strip non-printable characters string.whitespace"
    • Description: Remove non-printable characters using 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) 

More Tags

intel-edison read.table openpgp google-polyline architecture shadow-dom stub quickblox testbed azure-rm-template

More Programming Questions

More Tax and Salary Calculators

More Transportation Calculators

More Housing Building Calculators

More Gardening and crops Calculators