Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created November 26, 2012 14:19
Show Gist options
  • Select an option

  • Save h2rd/4148450 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/4148450 to your computer and use it in GitHub Desktop.
Encode string to utf8
import chardet
def encode(string):
if not string:
return ''
if not isinstance(string, unicode):
encoding = chardet.detect(string)
if not encoding['encoding']:
encoding['encoding'] = 'ascii'
string = string.decode(encoding['encoding']).encode('utf8')
return string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment