day03_04 字符集编码转换
encode2.7.py
# Author:Adminone for linux platform
#-*- coding:utf-8 -*-
import sys
print(sys.getdefaultencoding()) #获取默认编码ASCII
############################################################
'''
s = "你好" #utf8
print(s)
s_to_gbk = s.decode("UTF-8").encode("gbk")
print(s_to_gbk)
gbk_to_utf8 = s_to_gbk.decode("gbk").encode("UTF-8")
print(gbk_to_utf8)
'''
############################################################
'''
s = u"你好" #unicode
s_to_gbk = s.encode("gbk")
print(s_to_gbk)
gbk_to_utf8 = s_to_gbk.decode("gbk").encode("UTF-8")
print(gbk_to_utf8)
'''
############################################################
s = "你好" #utf8
s_to_gb2312 = s.decode("UTF-8").encode("gb2312")
# s_to_gb2312 = s.encode("gb2312")
print(s_to_gb2312)
gb2312_to_utf8 = s_to_gb2312.decode("gb2312").encode("UTF-8")
print(gb2312_to_utf8)
encode3.x.py
# Author:Adminone
s = "你好"
import sys
print(sys.getdefaultencoding()) #获取默认编码UTF-8
print(s)
s_utf8 = s.encode("utf-8")
print(s_utf8) #转为byte UTF-8
s_gbk = s.encode("gbk")
print(s_gbk) #转为byte gbk
gbk_to_utf8 = s_gbk.decode("gbk").encode("utf-8")
print(gbk_to_utf8)
#s_to_gbk = s.encode("utf-8").decode("utf-8").encode("gbk")
s_to_gbk = s_utf8.decode("utf-8").encode("gbk")
print(s_to_gbk)
encode3.x gb2312.py
#-*-coding:gb2312 -*- #这个也可以去掉
__author__ = 'Alex Li'
import sys
print(sys.getdefaultencoding())
msg = "我爱北京天安门"
#msg_gb2312 = msg.decode("utf-8").encode("gb2312")
msg_gb2312 = msg.encode("gb2312") #默认就是unicode,不用再decode,喜大普奔
gb2312_to_unicode = msg_gb2312.decode("gb2312")
gb2312_to_utf8 = msg_gb2312.decode("gb2312").encode("utf-8")
print(msg)
print(msg_gb2312)
print(gb2312_to_unicode)
print(gb2312_to_utf8)