python try和except是这么用的

if not ( type(sentence) is unicode):
        try:
            sentence = sentence.decode('utf-8')
        except:
            sentence = sentence.decode('gbk','ignore')

我想把字符串转换为unicode
先判断,如果不是unicode,尝试一下用utf-8解码,解码报错了,用gbk解码,如果也报错了,那么忽略报错。

你可能感兴趣的:(python try和except是这么用的)