Skip to content

Instantly share code, notes, and snippets.

@rtyler
Created June 26, 2009 21:04
Show Gist options
  • Select an option

  • Save rtyler/136737 to your computer and use it in GitHub Desktop.

Select an option

Save rtyler/136737 to your computer and use it in GitHub Desktop.
import Cheetah.Filters
class UnicodeHarder(Cheetah.Filters.Filter):
def filter(self, val,
encoding='utf8',
str=str,
**kw):
""" Try our best to unicode our strings """
if not val:
return u''
if isinstance(val, unicode):
return val
try:
return val.decode('utf-8', 'strict')
except UnicodeDecodeError:
try:
return val.decode('latin-1', 'strict')
except UnicodeDecodeError:
return val.decode('ascii', 'ignore')
except AttributeError:
return unicode(val)
return val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment