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


相关推荐

  • redis 源代码分析(一) 内存管理

    redis 源代码分析(一) 内存管理

    2021年11月30日
    36
  • 域名转接公告

    域名转接公告

    2021年7月25日
    72
  • 三十名网友共同自主研发粤语打字软件「建议收藏」

    三十名网友共同自主研发粤语打字软件「建议收藏」来源:羊城晚报 日期:2007-7-23  王许乐是厚街镇前进小学的语文教师。2005年底,他和网上其他29人一起用半年时间研发了一套粤语打字软件,在网友中大受欢迎,下载量过万。王许乐等人研发的这套轻松粤拼输入法目前已经推出了两个版本,他们正打算推出进一步改良版。 王许乐一直致力于粤语研究。2005年底,一个偶然的机会,他在网络上认识了一大批热爱粤语的人,大家一起交流从简单的粤语方言到省

    2022年7月16日
    13
  • Linux-awk数组

    Linux-awk数组linuxawk数组

    2022年7月19日
    14
  • 【网络】子网划分题目解析[通俗易懂]

    【网络】子网划分题目解析[通俗易懂]【网络】子网划分题目解题过程步骤一:划分分公司子网步骤二:划分分公司部门子网题目某集团公司,全国共设立12家分公司,每家分公司有4个部分组成,现在公司需要组建企业内部网络,总公司申请一个IP:172.16.0.0/16,试为该集团公司IP分配做出合理规划解题过程步骤一:划分分公司子网“全国共设立12家分公司”根据这句话可以明白至少要划分12个分公司子网,就需要我们查找2n≥12,依题意得24=16≥12,因此网络位向主机位借取4位,也就是用原有的16+4=20,那么子网掩码就是/20即2

    2022年6月27日
    26
  • macbook重设密码服务器错误_网页显示500错误

    macbook重设密码服务器错误_网页显示500错误在网上查了查解决方案如下:1。右键我的电脑–管理–本地用户和组,给IUSR_机器名和IWAM_机器名两个用户设置密码,要一样。2。开始–运行–打cmd,然后cdD:InetpubAdminscripts(我的系统在D盘),然后cscript.exeadsutil.vbssetw3svc/wamuserpass你的密码,然后cscript.exeadsutil.vbssetw…

    2022年8月12日
    3

发表回复

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

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