encoder和decoder的区别_decode作用

encoder和decoder的区别_decode作用I’veneverbeensurethatIunderstandthedifferencebetweenstr/unicodedecodeandencode.Iknowthatstr().decode()isforwhenyouhaveastringofbytesthatyouknowhasacertaincharacterenco…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

encoder和decoder的区别_decode作用

I’ve never been sure that I understand the difference between str/unicode decode and encode.

I know that str().decode() is for when you have a string of bytes that you know has a certain character encoding, given that encoding name it will return a unicode string.

I know that unicode().encode() converts unicode chars into a string of bytes according to a given encoding name.

But I don’t understand what str().encode() and unicode().decode() are for. Can anyone explain, and possibly also correct anything else I’ve gotten wrong above?

EDIT:

Several answers give info on what .encode does on a string, but no-one seems to know what .decode does for unicode.

解决方案

The decode method of unicode strings really doesn’t have any applications at all (unless you have some non-text data in a unicode string for some reason — see below). It is mainly there for historical reasons, i think. In Python 3 it is completely gone.

unicode().decode() will perform an implicit encoding of s using the default (ascii) codec. Verify this like so:

>>> s = u’ö’

>>> s.decode()

Traceback (most recent call last):

File “”, line 1, in

UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xf6′ in position 0:

ordinal not in range(128)

>>> s.encode(‘ascii’)

Traceback (most recent call last):

File “”, line 1, in

UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xf6′ in position 0:

ordinal not in range(128)

The error messages are exactly the same.

For str().encode() it’s the other way around — it attempts an implicit decoding of s with the default encoding:

>>> s = ‘ö’

>>> s.decode(‘utf-8’)

u’\xf6′

>>> s.encode()

Traceback (most recent call last):

File “”, line 1, in

UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xc3 in position 0:

ordinal not in range(128)

Used like this, str().encode() is also superfluous.

But there is another application of the latter method that is useful: there are encodings that have nothing to do with character sets, and thus can be applied to 8-bit strings in a meaningful way:

>>> s.encode(‘zip’)

‘x\x9c;\xbc\r\x00\x02>\x01z’

You are right, though: the ambiguous usage of “encoding” for both these applications is… awkard. Again, with separate byte and string types in Python 3, this is no longer an issue.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/185136.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 短信指令_手机自动发短信一串代码

    短信指令_手机自动发短信一串代码===========移动短信指令=================发送CXBX到10086,查询当月套餐剩余短信条数。发送CXGFX到10086,查询当月飞信GPRS套餐剩余流量。发送CXGTC到10086,查询当月GPRS套餐剩余流量。发送CXCCT到10086,查询当月超级畅听套餐剩余流量。发送CXGLL到10086,查询当月已使用的GPRS流量总和。发送CXDX120,查询当月可选计划…

  • oracle视图表怎么修改(oracle视图添加字段)

    一个朋友在回复的时候给出了一篇inthirties写的关于更新视图的帖子,简洁明了,转过来学习学习。===============================================================================Oracle里视图可以update吗?如果在网上做出这样一个问题调查,我想很多的网友朋友,都会不假思索的回答到,不行,视图是逻辑记录,并不…

  • 滴滴回应乐清顺风车乘客遇害事件:深感自责与愧疚

    滴滴回应乐清顺风车乘客遇害事件:深感自责与愧疚

  • 【CBIR】基于内容的图像检索技(CBIR)术相术介绍「建议收藏」

    【CBIR】基于内容的图像检索技(CBIR)术相术介绍「建议收藏」基于内容的图像检索技(CBIR)术相术介绍转载之:kezunhai 出处:http://blog.csdn.net/kezunhai        近20年来,计算机与信号处理领域如火如荼地发展着,随着普通计算机的性能不断地提高,人们对计算机处理信息的能力及要求不断地提高。传统的基于文本检索技术已经难以满足人们的需求,图片作为人们对周围世界的感知媒

  • Oracle number数据类型的使用[通俗易懂]

    Oracle number数据类型的使用[通俗易懂]需要首先明白有效位的含义:从左到右,从第一个不为零的数开始计数第一种情况:number后面都是两个正数,第一个数表示有效位,第二个数表示小数点后的位数(也就是精确度,需要进行四舍五入)例如number(2,1)存入的数据有1,0.1,1.666分析过程:存入1:要求有效位小于等于2,所以自动补充0,存入1实际上判断的是1.0是否符合条件,自然可以添加存入0….

  • 云大使推广中的常见热门问题「建议收藏」

    云大使推广中的常见热门问题「建议收藏」云大使推广中的常见热门问题

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号