1. 程式人生 > >Python2編碼判斷Demo

Python2編碼判斷Demo

def unicode_it(html):
    if isinstance(html, str):
        r = chardet.detect(html)
        if r["confidence"] >= 0.7:
            encoding = r["encoding"]
            if encoding.lower() == "gb2312":
                charset = charset_pattern.findall(html)
                if not charset or charset[0
].lower().strip() == "gbk": encoding = "gbk" print encoding s = html.decode(encoding) return s else: return html