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


相关推荐

  • 获取股票历史数据(网易163行情接口)[通俗易懂]

    获取股票历史数据(网易163行情接口)获取股票历史数据,通过网易163接口来获取数据,可以获取指数数据,也可以获取股票数据importpandasaspd#沪市前面加0,深市前面加1,比如0000001,是上证指数,1000001是中国平安defget_daily(code,start=’19900101′,end=”):url_mod=”http://quotes.money.163.com/service/chddata.html?code=%s&start=%s

    2022年4月17日
    54
  • 脑科学磁共振成像(MRI)初学者必看——功能脑网络、小世界网络、FDR校正、脑电信号频率变换、模板、假设检验、广义线性模型、独立成分分析、影像组学、任务态和静息态方法汇总「建议收藏」

    脑科学磁共振成像(MRI)初学者必看——功能脑网络、小世界网络、FDR校正、脑电信号频率变换、模板、假设检验、广义线性模型、独立成分分析、影像组学、任务态和静息态方法汇总「建议收藏」磁共振成像初学者必看一、浅谈功能脑网络二、不同模态脑网络的构建功能脑网络结构脑网络白质纤维束脑网络加权网络二值网络三、趣谈散点图与相关系数四、脑电信号频域变换五、fMRI中的FDR校正六、模板(mask)1、模板(mask)往往是与ROI联系在一起的2、mask作用的原理3、常见的mask七、假设检验和效果量八、组水平标准化九、由ALFF说开去十、计算机存取MRI影像的那些事十二、Linux基础命令十三、浅谈标准空间模板和空间变换一:标准空间模板二:空间变换十四、功能连接十五、大脑激活与功能连接的

    2022年7月24日
    51
  • linux rsyslogd cpu占用率高问题「建议收藏」

    linux rsyslogd cpu占用率高问题「建议收藏」最近有几次,linuxcentos7服务停了后,重启,再起一些应用后,查看top后,rsyslogdcpu占用率高问题,先说我这块怀疑导致的原因吧。原因很有可能是当前机器的系统盘挂载出现问题,或者系统盘有磁道坏了,导致,在启动某个软件时,一直在记录日志。现象top命令看下一:解决发现rsyslog可以理解为增强版的syslog,可以支持输出日志到各种数据库,使用RELP+TCP实现数据的传输,对目前的服务器服务而言,可以关闭该进程。#第一步:重启rsyslog服务,

    2022年8月15日
    14
  • android系统中toast是什么_Android个人资料简单布局

    android系统中toast是什么_Android个人资料简单布局老规矩,先上效果图吧主要实现了几种常用的方式:1.最基本的Toast系统自带Toast采用的是队列的方式,等当前Toast消失后,下一个Toast才能显示出来;原因是Toast的管理是在队列中,点击一次,就会产生一个新的Toast,要等这个队列中的Toast处理完,这个显示Toast的任务才算结束。 so~我们可以把Toast改成单例模式,没有Toast再新建它,这样也就…

    2022年9月13日
    3
  • 抽象思维能力

    抽象思维能力世界上的物质纷繁复杂,眼花缭乱。人最大的特点是容易被眼睛看到的物像所吸引,而止步不前,不去深入思考内部深层次的原理。毕竟,进化了几百万年的人类过程,只要看到果实或者猎物就足够喂饱肚子了,谁去管果实

    2022年8月5日
    5
  • 最详细AMD Ryzen CPU,VMware 15安装macOS 10.15.x Catalina 记录(第一篇)[通俗易懂]

    最详细AMD Ryzen CPU,VMware 15安装macOS 10.15.x Catalina 记录(第一篇)[通俗易懂]如何在非macOS电脑上体验macOS,目前我所知道的有两种方式:真机安装(难度大,本文介绍,感兴趣的童鞋可以自行搜索);虚拟机安装。不管是真机安装还是虚拟机安装,Intel的cpu在安装过程中遇到的坑相比AMD的cpu要少很多。所以本文不介绍如何在Intelcpu的pc上安装macOS,网上教程很多,读者可自行搜索。很多AMDcpu的朋友,在用虚拟机安装macOS的过程中,肯定遇到过一些问题,并且有些问题很棘手,会被困扰几天,那么本文将介绍如何在AMDcpu的电脑上安装mac

    2022年5月13日
    56

发表回复

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

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