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


相关推荐

  • 百度之星资格赛,hdu 4825 XOR SUM

    百度之星资格赛,hdu 4825 XOR SUM

    2021年11月23日
  • HSQL 学习笔记

    HSQL 学习笔记HSQL学习笔记1.    hsql 学习1.1.    学习目的本文档是针对hSQL 数据库方面的基础学习,为了使项目组成员能够达到使用hSQL 数据库的目的。1.2.    培训对象开发人员1.3.    常用词及符号说明常用词:hsql:一种免费的跨平台的数据库系统E:\hsqldb:表示是在dos 命令窗口下面1.4.    参考信息doc

    2022年9月22日
  • java的反射(reflection)机制_腹部反射区图解

    java的反射(reflection)机制_腹部反射区图解1.什么是java反射java的反射机制是指在运行状态中,      对于任意一个类,都能知道这个类的所有属性和方法;      对于任意一个对象,都能够调用它的任意一个属性和方法;      这种动态获取的类的信息,和动态调用对象的方法的功能就是反射机制。2.反射的作用     在运行时,构造任意一个类的对象      在运行时,判断任意一个类的完整结构,比如属性,方法,内…

    2022年10月25日
  • 8月8日—阴[通俗易懂]

    8月8日—阴[通俗易懂]昨天可以算是我们毕业以后第一次和同学聚会,虽然只有3个同学,都是在北京工作的同学,但是毕竟还是第一次聚会,说了很多话~~~~晚上没有车了,黑人就住在我那里。一晚上和我聊了好多的话,说他以前和他女朋友的感情经历。都说东北人能侃,结果聊到了晚上2点多。~~~~我还得上班/`/`~~~`到最后我是困得不行了~~~想到自己的宝宝,感觉好幸福,好开心。不要刻意的去强求什么,故意的去做作什么,对于感情,默默…

    2022年5月7日
  • Swift 学习Using Swift mix and match, network: 写rss读者

    Swift 学习Using Swift mix and match, network: 写rss读者

    2022年1月9日
  • 测试用例8大要素及额外10小要素【建议收藏】[通俗易懂]

    测试用例8大要素及额外10小要素【建议收藏】[通俗易懂] 测试用例八大要素  1.测试用例编号  由字母、字符、数字组合而成的字符串,有唯一性,易识别性。  eg:  1)系统测试:产品编号-ST-系统测试项名-系统测试子项名-编号  2)集成测试:产品编号-IT-集成测试项名-集成测试子项名-编号  3)单元测试:产品编号-UT-单元测试项名-单元测试子项名-编号  这样看到编号就可以知道是做的什么测试,测试的对象是什么,也方便维护。  2.测试项目  当前测试用例所在测试用例所属大类、被测需求、被测模块、被测单元等。 

    2022年6月28日

发表回复

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

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