Python 编码格式检测,可以使用 chardet ,

例如:

    

import urllib
rawdata = urllib.urlopen('http://www.google.cn/').read()
import chardet
print chardet.detect(rawdata) 输出结果是:
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
# win下命令行参数为gbk编码:star.gbk2unicode(sys.argv[1]) + u'也有'
def gbk2unicode(s):
return s.decode('gbk', 'ignore') # 脚本文件#coding:utf-8时默认不带u的字符串为utf8字符串:star.utf82unicode('我')
def utf82unicode(s):
return s.decode('utf-8', 'ignore') # 带u的字符串为unicode
# star.unicode2gbk(u'\u4e5f\u6709')
# star.unicode2gbk(u'也有')
def unicode2gbk(s):
return s.encode('gbk') # 带u的字符串为unicode
# star.unicode2utf8(u'\u4e5f\u6709')
# star.unicode2utf8(u'也有')
def unicode2utf8(s):
return s.encode('utf-8') # win下命令行参数为gbk编码:star.gbk2utf8(sys.argv[1]) + '也有'
def gbk2utf8(s):
return s.decode('gbk', 'ignore').encode('utf-8') def utf82gbk(s):
return s.decode('utf-8', 'ignore').encode('gbk')

Python UNICODE GBK UTF-8 之间相互转换的更多相关文章

  1. python datetime和unix时间戳之间相互转换

                                python datetime和unix时间戳之间相互转换 1.代码:    import time    import datetime # ...

  2. Python中字符串与字节之间相互转换

    Python中字符串与字节之间相互转换 ​ a = b"Hello, world!" # bytes object b = "Hello, world!" # ...

  3. 【JAVA编码】 JAVA字符编码系列二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换

    http://blog.csdn.net/qinysong/article/details/1179489 这两天抽时间又总结/整理了一下各种编码的实际编码方式,和在Java应用中的使用情况,在这里记 ...

  4. 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换

    我们最初学习计算机的时候,都学过ASCII编码. 但是为了表示各种各样的语言,在计算机技术的发展过程中,逐渐出现了很多不同标准的编码格式, 重要的有Unicode.UTF.ISO-8859-1和中国人 ...

  5. Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换(转载)

    第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换 1.函数介绍在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有关的两 ...

  6. JAVA字符编码二:Unicode,ISO-8859,GBK,UTF-8编码及相互转换

    第二篇:JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换   1.函数介绍 在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有 ...

  7. Python: 在Unicode和普通字符串之间转换

    Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding): <!-- Inject Script Filtered --> Toggle line nu ...

  8. Python——在Unicode和普通字符串之间转换

    1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASC ...

  9. python unicode和string byte

    python unicode 和string那 开发过程中总是会碰到string, unicode, ASCII, 中文字符等编码的问题, 每次碰到都要现搜, 很是浪费时间, 于是这次狠下心, 一定要 ...

随机推荐

  1. 向第一个 p 元素添加一个类

    This is a heading This is a paragraph. This is another paragraph. 向第一个 p 元素添加一个类 111 <html> &l ...

  2. 在Ubuntu 桌面版 12.04 LTS安装并运行SSH

    第一步:安装openssh-server #sudo apt-get install openssh-server 第二步:查看ssh服务是否已经运行,执行 #ps -e | grep ssh 执行完 ...

  3. ubuntu server 安装 mantis bug tracker 中文配置

    ubuntu server 安装 mantis bug tracker 中文配置 官网:http://www.mantisbt.org/ 一:安装: 1:进入到 apache2的网站目录: cd /v ...

  4. 该内存不能为written

    #include <stdio.h>int main(){ char *p = "abc"; printf("%s\n", p); *p = 'd' ...

  5. Nhibernate Criteria 多个or条件查询

    sql: select * from table t where (t.name like '%张三%' or t.schoolName like '%张三%' or t.cityname like ...

  6. javaweb笔记分享

    Lesson 1 一.eclipse工具的使用 1. java代码的位置 1) 选择工作空间 workspace  选择一个文件夹存放程序(代码) 不要用中文和空格 2) 新建一个java 工程(Pr ...

  7. SCSS 实用知识汇总

    1.变量声明 $nav-color: #F90; nav { //$width 变量的作用域仅限于{}内 $width: 100px; width: $width; color: $nav-color ...

  8. NI License Activator 用法

    双击打开后,看到这种界面,将白色方格通过鼠标右击点绿就能够了. NI <wbr>License <wbr>Activator <wbr>用法 可能会出现这样的情况, ...

  9. MySQL优化之——权限管理

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46763767 在mysql数据库中,有mysql_install_db脚本初始化权 ...

  10. c#委托是什么?事件是不是一种委托?

    C#的委托是CTS(公共类型系统)规定的5中类型之一(类类型.结构类型.接口类型.枚举类型.委托类型).它类似于c或c++中的函数的指针,但函数指针只能引用静态方法,而委托既能引用静态方法,也能引用实 ...