chinese GBK encode - KerwinKoo/KerwinKoo.github.io GitHub Wiki

python 中文字符编码 GBK

# !/usr/bin/python  
#coding=gbk  
string='我的'  

python的对字符的编解码

字符编码/解码函数:

unicode

这个是python的内建函数,位于unicode类。

unicode(string [, encoding[, errors]]) -> object

这个函数的作用是将string按照encoding的格式编码成为unicode对象。
省略参数将用python默认的ASCII来解码

decode

位于unicode类中。
 decode(...)
 |      S.decode([encoding[,errors]]) -> string or unicode
 |      
 |      Decodes S using the codec registered for encoding. 

decode和encode都可以用于常规字符串和unicode字符串

但是:

str.decode()和unicode.encode()是直接正规的使用。

unicode.decode()会先将unicode转化成str,然后再执行decode()。

这里面涉及隐式类型转化的问题
⚠️ **GitHub.com Fallback** ⚠️