Tuesday, March 12, 2013

Python: Remove Non-printable characters from string

Current post is on how to remove non-printable characters from string.


Need a function to remove non-printable characters in python.


Remove Non-printable function for ASCII String:


def    remove_ascii_non_printable(str)
      """Removes Non-printable chars from ASCII String"""
      return ''.join([ch for ch in str if ord(ch) > 31 and ord(ch) < 126 or ord(c) = = 9])

Remove Non-printable function for Unicode String:



def    remove_unicode_non_printable(str)
      """Removes Non-printable chars from ASCII String"""
      return ''.join([ch for ch in str if ord(ch) > 31 or ord(c) = = 9])



Appreciate your feedback via comments.

Thanks.

No comments:

Post a Comment

Disclaimer:

The above post and all the posts in the blog are derived from facts, information, logical interpretation and logical conclusion of printed and internet materials available to me, perceived and produced by 99 gm brain of mine, which by no means always be accurate, consistent and complete.

All the posts are for personal quick reference only.

If any suggestion, correction, misinterpretation, misconception commented, which will be moderated and deleted if required, to avoid unforeseen issues.

If any trademark / copywrite issue is present, do send in a mail and appropriate tag (logo, name, website link) will be attached to the same.

Additional disclaimer will be attached wherever required in the post.