Python编码UNICODE GBK UTF-8字符集转换的正确姿势
来源:互联网 发布:数据恢复精灵的注册码 编辑:程序博客网 时间:2024/06/07 08:34
规范
- 脚本文件编码
由于需要经常性使用到中文字符,因此Python脚本新建后,请在头部添加代码:
# coding: utf-8
或者为pycharm设置代码模版,这样每次新建Python文件时会自动带上以上代码。如果不添加,即使中文字符串以u开头,也是编译不通过的。
UNICODE
- 脚本中的字符最好使用UNICODE编码(英文可以不需要,但是中文尽量使用),因为UNICODE编码是比较好的“中间”字符集,比较容易向GBK和UTF-8转换。例如:
print u"你好"
如果不带u的字符在包含了# coding: utf-8的脚本中默认字符为UTF-8,一般也不会有什么问题。
- UNICODE转GBK:
# 带u的字符串为unicodes.encode('gbk')
- UNICODE转UTF-8
# 带u的字符串为unicodes.encode('utf-8')
GBK
- GBK转UNICODE
Windows下的命令行参数为GBK编码,因此需要对字符串进行转换,转换方法有两种。
方法一:
# Create a new Unicode object from the given encoded string.# encoding defaults to the current default string encoding.# errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.unicode(s, "gbk", "ignore")
方法二:
s.decode('gbk', 'ignore')
跨平台判断:
if sys.platform == 'win32': # win下命令行参数为gbk编码,转换字符 passelse: pass
UTF-8
- UTF-8转换为UNICODE
unicode(s, "utf-8", "ignore")
或:
s.decode('utf-8', 'ignore')
汇总整理成函数库
# 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')
1 2
- Python编码UNICODE GBK UTF-8字符集转换的正确姿势
- UNICODE, GBK, UTF-8编码的转换
- python 之编码转换 unicode, utf-8, utf-16, GBK
- java 字符集与编码 unicode、utf-8、utf-16、gbk
- unicode Ascii gbk字符集和utf-8 utf-16编码
- Python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换。
- Python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换
- c# GBK,Unicode,UTF-8编码与汉字的转换
- 字符编码Unicode UTF-8 GBK的不同和转换
- Python中GBK, UTF-8和Unicode的编码问题
- Python中GBK, UTF-8和Unicode的编码问题
- ASCII 、GB2312、GBK、GB18030、unicode、UTF-8字符集编码详解
- ASCII 、GB2312、GBK、GB18030、unicode、UTF-8字符集编码介绍
- ASCII 、GB2312、GBK、GB18030、unicode、UTF-8字符集编码详解
- ASCII 、GB2312、GBK、GB18030、unicode、UTF-8字符集编码详解
- ASCII 、GB2312、GBK、GB18030、unicode、UTF-8字符集编码详解
- ASCII 、GB2312、GBK、GB18030、unicode、UTF-8字符集编码详解
- 字符集编码:ASCII 、GB2312、GBK、GB18030、UTF-8、unicode
- 实现一个双缓冲队列
- HDOJ题目分类
- Xshell配置SSH密钥登录Linux服务器
- SPOJ Antisymmetry 回文串manacher
- 正则表达式
- Python编码UNICODE GBK UTF-8字符集转换的正确姿势
- java-用main方法连接数据库测试
- python特性(六):yield与迭代器生成器
- java元注解
- pat-a1100. Mars Numbers (20)
- POJ 3429 Geometry with a ruler 可能会
- HDU 2086 A1=? [数学问题]
- 利用css话三角
- BZOJ3437: 小P的牧场