python编码转换
把其他编码转换为unicode,比如转换utf-8编码:
unicode_text = unicode(utf8_text, 'utf-8')
把unicode编码转换为其他编码,比如gb18030编码:
gb18030_text = unicode_text.encode('gb18030')
以上两者结合,可以转换utf-8到gb18030编码:
gb18030_text = unicode(utf8_text, 'utf-8').encode('gb18030')
对于Windows平台,如果要转换到ANSI(简体中文就是gb18030,繁体中文big5,…)则可以使用mbcs编码(mbcs即:Multi-Byte Character Set):
mbcs_text = unicode(utf8_text, 'utf-8').encode('mbcs')
参考文档:
- 字符编码和python使用encode,decode转换utf-8, gbk, gb2312 - jxzheng - 博客园
- 解决UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-3问题 - mingaixin - 博客园
- 【已解决】UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multibyte sequence | 在路上
- Python, Windows, Ansi - encoding, again - Stack Overflow
- 4.9.2 Standard Encodings
- 28.1. sys — System-specific parameters and functions — Python 2.7.13 documentation
本文介绍了如何使用Python进行不同字符编码之间的转换,包括从UTF-8到Unicode,再从Unicode到GB18030或其他编码的过程。同时也提到了在Windows平台上如何将UTF-8编码转换为ANSI编码的方法。
463

被折叠的 条评论
为什么被折叠?



