【编码转换】python的编码转换

一丶python2.7编码转换

解码:

字符串.decode('指定编码')  #解码,需要指定原来是什么编码

编码:

解码后的字符串.encode('指定编码') #编码需要解码的字符串

代码示例:

 
# -*- coding:utf-8 -*-
#编码转换
sttr="阿斯顿发斯蒂芬"   #utf-8
str_decode=sttr.decode("utf-8")
str_encode=str_decode.encode("gbk")  #主要如果这里没有解码字符串str_decode,直接用sttr来编码代码会直接报错
print(str_encode)

总结:

编码和解码主要是用于编码的互相转换

二丶在python3中

自动转换 utf-8,unicode,gbk

python3中移除了unicode类型

代码示例:

tempstr="阿斯蒂芬"
#自动转换print(temp_gbk)

 

posted @ 2017-05-31 11:20  丰study  阅读(139)  评论(0)    收藏  举报