Skip to content

Instantly share code, notes, and snippets.

@tkaemming
Created March 12, 2012 22:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkaemming/2025129 to your computer and use it in GitHub Desktop.
Save tkaemming/2025129 to your computer and use it in GitHub Desktop.
template tag to strip basic unicode control characters (unprintables, usually entered as artifacts of bad encoding/decoding from iso-8859 subsets like windows-1252) from template output
import re
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
CONTROL_CHARACTERS = re.compile(r'([\x00-\x1f\x7f-\x9f])*')
@register.filter
@stringfilter
def strip_control_characters(value):
"""Strips Unicode control characters from input."""
return CONTROL_CHARACTERS.sub('', value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment