汉字格式的自动侦测

可用chardet模块自动侦测汉字的编码模式(utf-8或GBK)

首先下载chardet模块,然后安装

测试用例如下:

 1 #!/usr/bin/env python
 2 # -*- coding: UTF8 -*-
 3 # -*- coding: GBK-*-
 4 # 这些
 5 import chardet
 6 codedetect=chardet.detect("我们")["encoding"]
 7 try:
 8     ustring=unicode("我们",codedetect)
 9     print ustring    
10 except:
11     print "error"
原文地址:https://www.cnblogs.com/djcsch2001/p/2729086.html