Python: String encode

string encode from ‘utf-8’ to ‘cp932’ avoiding “UnicodeDecodeError”

# -*- coding: utf-8 -*-

ENCODE_TO = 'cp932'

L = ['a', '漢字', u'漢字', 1, None]

for c in L:
    if isinstance(c, unicode):
        print c.encode(ENCODE_TO)
    elif isinstance(c, str):  
        # when element is str, firstly decode to unicode object and then encode
        print c.decode('utf-8').encode(ENCODE_TO, 'replace')
        print str(c)


Leave a Reply

Your email address will not be published. Required fields are marked *