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.

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

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

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

  • VBA listview控件「建议收藏」

    VBA listview控件「建议收藏」
    1、在Listview控件中,用ColumnHeaders对象来操作列,而添加新的列可以用ColumnHeaders对象的ADD方法。具体如下: 
    ListView1.ColumnHeaders.Add序号,唯一的字符串标识,列标显示文字,列宽,列的内容对齐方式,所使用的图标序号。
    对齐方式有:lvwColumnLeft 、 lvwColumnCenter、lvwColumnRight                                            

  • 代码在线编辑工具_php代码编辑器安卓版

    代码在线编辑工具_php代码编辑器安卓版在线代码编辑器    在线编辑各种文本形式的源代码,如js,html,php等,要支持语法高亮,即时输入即时高亮。 我的初步想法是用一个来实现,就是类似于常见的在线网页编辑器,但是因为仅仅是需要代码编辑,所以,要控制只能输入文本,不能让用户插入图片啊链接啊等等东西,感觉比较难。 希望大家能够探讨一下如何实现。 Bespin

  • CMD命令:不是内部或者外部命令也不是可运行的程序或批处理文件

    CMD命令:不是内部或者外部命令也不是可运行的程序或批处理文件前言:相信有很多小伙伴都比较喜欢使用Command命令来快速的打开或运行程序,但是有些时候命令提示符会和我们开个小玩笑。今天我就教大家如何管教这个不听话的cmd!场景:看有些大神在命令提示符里输入两句命令就能执行一大串东西,本着学习的态度,先试试再说!没成想出现了:“不是内部或外部命令,也不是可运行的程序或批处理文件。”通过各种查各种找,终于…

  • Java 发送邮件的几种方式[通俗易懂]

    Java 发送邮件的几种方式[通俗易懂]发送文件的项目地址(free):https://download.csdn.net/download/qq_36474549/10741073导入jar包:activation-1.1.jar  javax.mail-1.6.2.jar内容:1.发送一封只包含文本的简单邮件   SendEmail_text.java      2.发送包含内嵌图片的邮件   Send…

  • oracle declare多个变量_赋值

    oracle declare多个变量_赋值方法1:declare@aint=4declare@bvarchar(100)=’testsql’方法2:declare@aint,@bvarchar(100)set@a=4set@b=’sqltest’方法3:declare@aint=t,@bvarchar(100)=’sqltest’个人感觉方法3好用,减少冗余~~~…

  • poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)[通俗易懂]

    poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)

发表回复

您的电子邮箱地址不会被公开。

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