python3 中encode 和decode的使用方法。

编码:

  将文本转换成字节流的过程。即Unicode----------->特定格式的编码方式,产生特定的字节流保存在硬盘中(一般为utf-8格式)。

解码:

  将硬盘中的字节流转换成文本的过程。即特定格式的字节流------------->Unicode。

注意:

  在内存中写的所有的字符,一视同仁,都是Unicode编码,但只有往硬盘保存或者基于网络传输时,才能确定你输入的字符是英文还好汉文,这就是Unicode转换成其他编码格式的过程。

在Python3中的字符串类型:

  文本字符串类型:

    即我们通常定义的str类型的对象。在Python3中,str类型的对象都是Unicode,因此对于str类型的对象只有encode()方法,没有decode()方法(若运行,会报错)。

  字节字符串类型:

    即byte类型的对象。对于该类对象,是由str类型对象使用encode()方法产生,byte对象可以进行解码过程,从而得到真正的内容。

避免出现乱码的准则:

  遵循编码使用哪种格式,解码就使用哪种格式。

时间: 2024-08-24 02:45:55

python3 中encode 和decode的使用方法。的相关文章

python3中encode和decode的一些基本用法

python3中encode和decode跟python2还是有一定的区别的,在python3中: encode(编码):按照某种规则将"文本"转换为"字节流".  python 3中表示:unicode变成str decode(解码):将"字节流"按照某种规则转换成"文本".   python3中表示:str变成unicode 字符串在Python内部的表示是Unicode编码,因此在做编码转换时,通常需要以Unicode作

js中encode、decode的应用说明

escape 方法 返回一个可在所有计算机上读取的编码 String 对象. function escape(charString : String) : String 参数 charString 必选.要编码的任何 String 对象或文本. 备注 escape 方法返回一个包含 charstring 内容的字符串值(Unicode 格式).所有空格.标点.重音符号以及任何其他非 ASCII 字符都用 %xx 编码替换,其中 xx 等于表示该字符的十六进制数.例如,空格返回为“ ”. 字符值大

python3 str.encode bytes.decode

str.encode 把字符串编码成字节序列 bytes.decode 把字节序列解码成字符串 https://docs.python.org/3.5/library/stdtypes.html str.encode(encoding="utf-8", errors="strict") Return an encoded version of the string as a bytes object. Default encoding is 'utf-8'. err

【python】Python3中出现'gbk' codec can't encode characte的成功解决方法?

亲身测试,所遇问题完全解决!2018/07/08 21:37 环境:windows,Pycharm,python3.6.2 使用Python写文件的时候,或者将网络数据流写入到本地文件的时候,大部分情况下会遇到:UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position ... 这个问题. 网络上有很多类似的文件讲述如何解决这个问题,但是无非就是encode,decode相关的,这是导致该问题出现的真正原因吗

Python3中出现UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)的解决方法

添加代码 import sys import codecs sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach()) Python3中出现UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)的解决方法 原文地址:https://www.cnblogs.com/liangxc/p/10228027.html

python3中使用builtwith的方法(很详细)

1. 首先通过pip install builtwith安装builtwith C:\Users\Administrator>pip install builtwith Collecting builtwith Downloading builtwith-1.3.2.tar.gz Installing collected packages: builtwith Running setup.py install for builtwith ... done Successfully install

python的str,unicode对象的encode和decode方法

python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byte[]. 而python中的unicode对象应该才是等同于java中的String对象,或本质上是java的char[]. 对于 s="你好" u=u"你好" s="你好" u=u"你好" 1. s.decode方法和u.enc

Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)

在 linux服务器上运行代码报错: Python3中遇到UnicodeEncodeError: ‘ascii’ codec can’t encode characters in ordinal not in range(128) 但是在windows上面运行代码正常. 原因是因为:linux系统语言导致的. 查看了一下系统环境编码 >>> import sys>>> sys.stdout.encoding'US-ASCII' 而另一台能正常打印的机器是 en_US.U

【转】Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128)

[转]Python3中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in ordinal not in range(128) 现象 打印任何一种包含有中文的对象,字典.列表.DataFrame.或字符串.比如: print('中文') 控制台报错: Traceback (most recent call last): File "printcn.py", line 1, in <module> pri