Created
November 26, 2012 14:19
-
-
Save h2rd/4148450 to your computer and use it in GitHub Desktop.
Encode string to utf8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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