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.

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • windows通过cmd查找端口占用

    windows通过cmd查找端口占用windows通过cmd查找端口占用1.查看端口占用情况netstat-ano2.查看某个端口被占用情况netstat-ano|findstrxxxx//xxxx为查询的端口号3.查看某个状态的端口netstat-ano|findstrxxxx//xxxx为查询的端口状态具体端口状态请查找后面描述。…

    2022年5月19日
    45
  • pycharm中代码补全,代码提示功能「建议收藏」

    pycharm中代码补全,代码提示功能「建议收藏」那什么,,,,,,是这样的,请先确保你的代码补全功能是打开的。打开操作方式是:file—->powersavemode,把这个前面的√号去掉即可。然后,代码在提示的时候,多打几个字,发现你想要的已经在最上面的时候按tab键即可补全

    2022年8月29日
    2
  • 最全的数据查询网站「建议收藏」

    最全的数据查询网站「建议收藏」1、国家数据——主要用户:社会情况研究人员国家统计局开设网站,公布我国各个领域的宏观经济情况,权威度高2、中国裁判文书网——主要用户:法律从业/学习/爱好者中国最高人民法院开设,权威可信,可用于查询国内裁判文书,可作数据统计来源3、中国互联网数据平台——主要用户:互联网研究人员经国家主管部门批准组建的管理和服务机构,经常发布一些有价值的互联网信息报告4、中国信通院——主要用户:互…

    2022年6月17日
    41
  • 【CSS使用技巧】[通俗易懂]

    最近,我开始升级网志了。在修改模板的过程中,需要重写CSS样式表。…

    2022年1月18日
    45
  • 最小二乘法详解_通俗是啥意思

    最小二乘法详解_通俗是啥意思本文转自https://blog.csdn.net/bitcarmanlee/article/details/51589143,谢谢原作者辛苦整理。若侵权,告知即删。最小二乘是每个上过大学的同学都接触过的概念与知识点(当然可能纯文科的同学没接触过,但是一般纯文科的同学也不会看这篇文章好像)。最小二乘理论其实很简单,用途也很广泛。但是每次说到最小二乘,总感觉差了点什么似的,好像对于最小二乘的前世…

    2022年8月30日
    3
  • 腾讯云视频直播sdk开发攻略

    腾讯云视频直播sdk开发攻略视频直播这一两年在移动互联网上可以说是非常的火,各种视频直播软件层出不穷。有的通过自己的推广宣传确实火了起来,比如:映客。我之前也是在一家专门做视频直播的公司打酱油,当时对这个概念还是很模糊,后来才慢慢的了解清楚视频直播的这个概念。后来离开这家公司,到现在的公司,做了一段时间,又有需要做视频直播的需求。由于公司各方面原因,只能引用第三方的sdk,最后选择的腾讯云。所以我下面给大家讲一下开发过程

    2022年6月9日
    51

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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