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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • cuDNN安装教程(Windows)「建议收藏」

    cuDNN安装教程(Windows)「建议收藏」简介NVIDIACUDA®深度神经网络库(cuDNN)是一个用于深度神经网络的GPU原始加速库。cuDNN为标准例程(如前向和反后卷积、池化、归一化和激活层)提供了高度调整的实现。世界各地的深度学习研究人员和框架开发人员依靠cuDNN获得高性能GPU加速。它允许他们专注于培训神经网络和开发软件应用程序,而不是花时间在低级别的GPU性能调整上。cuDNN加速广泛使用的深度学习框架,包括Caffe2、Chainer、Keras、MATLAB、MxNet、PaddlePaddle、PyTorch和T

    2022年6月6日
    105
  • pytorch版本对应关系

    pytorch版本对应关系https://blog.csdn.net/baidu_20163013/article/details/105470332

    2022年6月20日
    45
  • Java旅游管理系统

    Java旅游管理系统基于Java的SSM旅游平台基于Java的SSM旅游平台功能介绍系统部分快照基于Java的SSM旅游平台功能介绍(1)用户注册,登陆,验证,退出等基本功能;(2)景点介绍,天气预报,门票预订,酒店预订;(3)旅游攻略,旅游咨询,驴友风采;(4)景点地图;(5)驴友社区;(8)个人旅途分享;系统部分快照后台管理界面图片省略…

    2022年6月11日
    36
  • 万能乘法速算法大全_哪位老师整理的数学公式大全以及小学速算技巧,这么全?赶紧为孩子存下!…

    万能乘法速算法大全_哪位老师整理的数学公式大全以及小学速算技巧,这么全?赶紧为孩子存下!…于茫茫书海中,为你寻找更适合自己成长的有效资源和那些锲入心灵的文字。与高人交心,轻松学习,把时间留给更重要的人更重要的事。精彩就点击右上角分享出去,赠人玫瑰手染余香。上册预习专区小学1-6语文课内:第1课第2课第3课第4课第5课第6课第7课第8课第9课单元1练习:第1课第2课第3课第4课第5课作业1第6课第7课第8课作业2第9课生字:1-3年级生字…

    2022年6月7日
    33
  • 磁盘阵列恢复方法以及注意事项

    磁盘阵列恢复方法以及注意事项想要弄清楚磁盘阵列恢复,首先就得知道什么是磁盘阵列,磁盘阵列多用于存储服务器,数据服务器等企业级大数据存储领域,磁盘阵列是把多块独立的物理硬盘按不同方式组合起来形成一个逻辑硬盘,当磁盘瘫痪或硬件损坏后,为了恢复存储在阵列平台的数据被称之为磁盘阵列数据恢复,而磁盘阵列能够提供比单个硬盘有着更高的性能和提供数据冗余的技术。  对于做数据恢复人员来讲,最怕的是什么,那就是在客户发现数据丢失后,没有停

    2022年4月29日
    90
  • 百度云网页视频加速播放

    百度云网页视频加速播放最近使用百度云看考研视频,但是百度云的播放器太简陋,无法倍速播放视频,太浪费时间了,听说firefox可以加速播放,可我又是chrome的死忠,于是在网上查到一个简单的方法。进入播放页面,按F12进入开发者模式在console中输入:videojs.getPlayers("video-player").html5player.tech_.setPlaybackRate(2…

    2022年5月24日
    36

发表回复

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

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