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)
上一篇 2022年10月6日 下午10:46
下一篇 2022年10月6日 下午10:46


相关推荐

  • 跨境亚马逊FBA系统课程,精准布局跨境电商,亚马逊跨境之旅从这里开始(更新)

    跨境亚马逊FBA系统课程,精准布局跨境电商,亚马逊跨境之旅从这里开始(更新)

    2026年3月13日
    3
  • python自动化测试—Python自动化框架及工具

    python自动化测试—Python自动化框架及工具1概述手续的关于测试的方法论,都是建立在之前的文章里面提到的观点:功能测试不建议做自动化接口测试性价比最高接口测试可以做自动化后面所谈到的测试自动化也将围绕着接口自动化来介绍。本系列选择的测试语言是python脚本语言。由于其官方文档已经对原理有了比较清楚的解释,本文就不做一些多余的翻译工作了。偏向于实战部分,而且为了偏向实战,也会结合IDE工具和项目组织来进行讲解。理由如下:1.脚本语言,开发和迭代的效率极高2.第三方的扩展库极多,有很我现成的工具可以使用在正式进

    2025年5月28日
    4
  • lamp配置多个虚拟站点

    lamp配置多个虚拟站点在同一ip下添加多个域名站点!1.查看ip命令:ifconfig2.添加域名命令:vi/etc/hosts输入域名:如192.168.160.127www.test.com192.168.160.127www.test2.com3.创建要配置站点的文件夹及文件.var/www/test/index.php/var/www/test2/inde…

    2022年6月5日
    29
  • js 打印 三角形

    js 打印 三角形js 编辑三角形在 js 中每个图案站位的大小不一向新的改变我们对 Markdown 编辑器进行了一些功能拓展与语法支持 除了标准的 Markdown 编辑器功能 我们增加了如下几点新功能 帮助你用它写博客 全新的界面设计 将会带来全新的写作体验 在创作中心设置你喜爱的代码高亮样式 Markdown 将代码片显示选择的高亮样式进行展示 增加了图片拖拽功能 你可以将本地的图片直接拖拽到编辑区域直接展示 全新的 KaTeX 数学公式语法 增加了支持甘特图的 mermaid 语法 1 功能 增加了多屏

    2026年3月18日
    1
  • AI Agent记忆系统:从感官记忆到扣子平台实践

    AI Agent记忆系统:从感官记忆到扣子平台实践

    2026年3月14日
    2
  • C语言中volatile关键字的作用

    C语言中volatile关键字的作用一.前言1.编译器优化介绍:由于内存访问速度远不及CPU处理速度,为提高机器整体性能,在硬件上引入硬件高速缓存Cache,加速对内存的访问。另外在现代CPU中指令的执行并不一定严格按照顺序执行,没有相关性的指令可以乱序执行,以充分利用CPU的指令流水线,提高执行速度。以上是硬件级别的优化。再看软件一级的优化:一种是在编写代码时由程序员优化,另一种是由编译器进行优化。编译器优化常用的方法有:将内存变

    2022年6月1日
    39

发表回复

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

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