(十一)python3 encode()和decode()
从英文意思上看,encode和decode分别指编码和解码。在python中,Unicode类型是作为编码的基础类型,即:
decode encode
str ---------> str(Unicode) ---------> str
>>> u = '中文' # 指定字符串类型对象u
>>> str1 = u.encode('gb2312') # 以gb2312编码对u进行编码,获得bytes类型对象
>>> print(str1)
b'\xd6\xd0\xce\xc4'
>>> str2 = u.encode('gbk') # 以gbk编码对u进行编码,获得bytes类型对象
>>> print(str2)
b'\xd6\xd0\xce\xc4'
>>> str3 = u.encode('utf-8') # 以utf-8编码对u进行编码,获得bytes类型对象
>>> print(str3)
b'\xe4\xb8\xad\xe6\x96\x87'
>>> u1 = str1.decode('gb2312') # 以gb2312编码对字符串str进行解码,获得字符串类型对象
>>> print('u1')
'中文'
>>> u2 = str1.decode('utf-8') # 报错,因为str1是gb2312编码的
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 0: invalid continuation byte
(十一)python3 encode()和decode()的更多相关文章
- 在python3 encode和decode 的使用
说这个问题之前必须的介绍关于编码的在我们这的发展: 首先电脑能识别的最初的语言是二进制 ---010101这种 然后在是我们知道的ASSIC码 再过了就是 gb2312----------->g ...
- python3的encode()和decode()
python3的encode()和decode() 在python3的内存中. 在程序运行阶段. 使⽤用的是unicode编码. 因为unicode是万国码. 什么内容都可以进行显示. 那么在数据传输 ...
- python encode和decode函数说明【转载】
python encode和decode函数说明 字符串编码常用类型:utf-8,gb2312,cp936,gbk等. python中,我们使用decode()和encode()来进行解码和编码 在p ...
- [转]python新手必碰到的问题---encode与decode,中文乱码--转载
edu.codepub.com/2009/1029/17037.php 这个问题在python3.0里已经解决了. 这有篇很好的文章,可以明白这个问题: 为什么会报错“UnicodeEncodeErr ...
- Python学习-is和==区别, encode和decode
一.is 和 == 介绍 1. is 比较的是两个对象的内存地址是否相同,它们是不是同一个对象. 2. == 比较的是两个对象的内容是否相同. 在使用is前,先介绍Python的一个内置函数id( ...
- python 的 encode 、decode、字节串、字符串
一.摆个图 DJ DJ DJ Decode. J 解码 首先得知道字符串有哪些编码格式,至于为什么会有这么多的编码格式,以后再了解更新. 1.ASCII 占1个字节,只支持英文 2.GB231 ...
- python的str,unicode对象的encode和decode方法, Python中字符编码的总结和对比bytes和str
python_2.x_unicode_to_str.py a = u"中文字符"; a.encode("GBK"); #打印: '\xd6\xd0\xce\xc ...
- 关于编码和解码问题——encode、decode
一.背景和问题 近期在做一个关于声卡录音的项目,开发环境是win10 64位家庭中文版,pycharm2019.1,python3.6(Anaconda3),python模块pyaud ...
- [LeetCode] Encode and Decode Strings 加码解码字符串
Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...
随机推荐
- 51nod 1188 最大公约数之和 V2
第二个\( O(T\sqrt(n)) \)复杂度T了..T了..T了...天地良心,这能差多少?! 于是跑去现算(. \[ \sum_{i=1}^{n-1}\sum_{j=i+1}^{n}gcd(i, ...
- Word Cloud (词云) - Python
>>What's Word Cloud 词云 (Word Cloud)是对文本中出现频率较高的词语给予视觉化展示的图形, 是一种常见的文本挖掘的方法.目前已有多种数据分析工具支持这种图形, ...
- 【TIDB】2、TIDB进阶
0.TIDB优势 1.和MySql相比,具备OLAP能力.省去了很多数据仓库搭建成本和学习成本.这在业务层是非常受欢迎的.可以在其他分库分表业务中,通过 syncer 同步,进行合并,然后进行统计分析 ...
- [GZOI2016] 亚索的量子实验【分块】
第二题 亚索的粒子实验 [问题描述] 亚索是一名伟大的科学家,他最近在做一个粒子的实验,粒子初始有一定的能量,实验过程中倘若第i个粒子被注入k能量,那该粒子就会增加k能量,同时由于辐射作用,第2i,3 ...
- 水题 Codeforces Round #307 (Div. 2) A. GukiZ and Contest
题目传送门 /* 水题:开个结构体,rk记录排名,相同的值有相同的排名 */ #include <cstdio> #include <cstring> #include < ...
- 题解报告:hdu 1171 Big Event in HDU(多重背包)
Problem Description Nowadays, we all know that Computer College is the biggest department in HDU. Bu ...
- 题解报告:poj 2299 Ultra-QuickSort(BIT求逆序数)
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- 自己制作ssl证书
首先执行如下命令生成一个key openssl genrsa -des3 -out ssl.key 1024 然后他会要求你输入这个key文件的密码.不推荐输入.因为以后要给nginx使用.每次r ...
- AJPFX关于throw、throws关键字的解析
throw.throws关键字 throw关键字: 是用于方法体内部,用来抛出一个Throwable类型的异常.如果抛出了检查异常, 则还应该在方法头部声明方法可能抛出的异常类型.该方法的调用者也必须 ...
- (四)SpringIoc之Bean装配
在pom.xml的依赖 <dependencies> <!--测试包--> <dependency> <groupId>junit</groupI ...