孤独的猫

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

可用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"

 

posted on 2012-10-18 10:54  孤独的猫  阅读(222)  评论(0编辑  收藏  举报