python encode和decode函数说明

来源:互联网 发布:java环境变量配置win10 编辑:程序博客网 时间:2024/05/18 03:04

字符串编码常用类型:utf-8,gb2312,cp936,gbk等。

Python中,我们使用decode()和encode()来进行解码和编码

在python中,使用unicode类型作为编码的基础类型。即

     decode              encode

str ---------> unicode --------->str

u = u'中文' #显示指定unicode类型对象ustr = u.encode('gb2312') #以gb2312编码对unicode对像进行编码
str1 = u.encode('gbk')
#以gbk编码对unicode对像进行编码
str2 = u.encode('utf-8') #以utf-8编码对unicode对像进行编码u1 = str.decode('gb2312')#以gb2312编码对字符串str进行解码,以获取unicodeu2 = str.decode('utf-8')#如果以utf-8的编码对str进行解码得到的结果,将无法还原原来的unicode类型

如上面代码,str\str1\str2均为字符串类型(str),给字符串操作带来较大的复杂性。

好消息来了,对,那就是python3,在新版本的python3中,取消了unicode类型,代替它的是使用unicode字符的字符串类型(str),字符串类型(str)成为基础类型如下所示,而编码后的变为了字节类型(bytes)但是两个函数的使用方法不变:

     decode              encode

bytes ------> str(unicode)------>bytes

u = '中文' #指定字符串类型对象ustr = u.encode('gb2312') #以gb2312编码对u进行编码,获得bytes类型对象stru1 = str.decode('gb2312')#以gb2312编码对字符串str进行解码,获得字符串类型对象u1u2 = str.decode('utf-8')#如果以utf-8的编码对str进行解码得到的结果,将无法还原原来的字符串内容

避免不了的是,文件读取问题:

假如我们读取一个文件,文件保存时,使用的编码格式,决定了我们从文件读取的内容的编码格式,例如,我们从记事本新建一个文本文件test.txt, 编辑内容,保存的时候注意,编码格式是可以选择的,例如我们可以选择gb2312,那么使用python读取文件内容,方式如下:

复制代码
f = open('test.txt','r')s = f.read() #读取文件内容,如果是不识别的encoding格式(识别的encoding类型跟使用的系统有关),这里将读取失败'''假设文件保存时以gb2312编码保存'''u = s.decode('gb2312') #以文件保存格式对内容进行解码,获得unicode字符串'''下面我们就可以对内容进行各种编码的转换了'''str = u.encode('utf-8')#转换为utf-8编码的字符串strstr1 = u.encode('gbk')#转换为gbk编码的字符串str1str1 = u.encode('utf-16')#转换为utf-16编码的字符串str1
复制代码

python给我们提供了一个包codecs进行文件的读取,这个包中的open()函数可以指定编码的类型:

import codecsf = codecs.open('text.text','r+',encoding='utf-8')#必须事先知道文件的编码格式,这里文件编码是使用的utf-8content = f.read()#如果open时使用的encoding和文件本身的encoding不一致的话,那么这里将将会产生错误f.write('你想要写入的信息')f.close()

Python decode()方法


Python decode() 方法以 encoding 指定的编码格式解码字符串。默认编码为字符串编码。

语法

decode()方法语法:

str.decode(encoding='UTF-8',errors='strict')

参数

  • encoding -- 要使用的编码,如"UTF-8"。
  • errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。

返回值

该方法返回解码后的字符串。

实例

以下实例展示了decode()方法的实例:

#!/usr/bin/pythonstr = "this is string example....wow!!!";str = str.encode('base64','strict');print "Encoded String: " + str;print "Decoded String: " + str.decode('base64','strict')

以上实例输出结果如下:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=Decoded String: this is string example....wow!!!

Python encode()方法



描述

Python encode() 方法以 encoding 指定的编码格式编码字符串。errors参数可以指定不同的错误处理方案。

语法

encode()方法语法:

str.encode(encoding='UTF-8',errors='strict')

参数

  • encoding -- 要使用的编码,如"UTF-8"。
  • errors -- 设置不同错误的处理方案。默认为 'strict',意为编码错误引起一个UnicodeError。 其他可能得值有 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' 以及通过 codecs.register_error() 注册的任何值。

返回值

该方法返回编码后的字符串。

实例

以下实例展示了encode()方法的实例:

#!/usr/bin/pythonstr = "this is string example....wow!!!";print "Encoded String: " + str.encode('base64','strict')

以上实例输出结果如下:

Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE=



0 0
原创粉丝点击