Python 3 - 编码/解码与字节/Str

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14472650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:35:44  来源:igfitidea点击:

Python 3 - Encode/Decode vs Bytes/Str

pythonpython-3.x

提问by if __name__ is None

I am new to python3, coming from python2, and I am a bit confused with unicode fundamentals. I've read some good posts, that made it all much clearer, however I see there are 2 methods on python 3, that handle encoding and decoding, and I'm not sure which one to use.

我是 python3 的新手,来自 python2,我对 unicode 基础有点困惑。我读过一些不错的帖子,这让一切变得更加清晰,但是我看到 python 3 上有 2 种方法可以处理编码和解码,但我不确定要使用哪一种。

So the idea in python 3 is, that every string is unicode, and can be encoded and stored in bytes, or decoded back into unicode string again.

因此,python 3 中的想法是,每个字符串都是 unicode,并且可以以字节为单位进行编码和存储,或者再次解码回 unicode 字符串。

But there are 2 ways to do it:
u'something'.encode('utf-8')will generate b'bytes', but so does bytes(u'something', 'utf-8').
And b'bytes'.decode('utf-8')seems to do same thing as str(b'', 'utf-8').

但是有两种方法可以做到:
u'something'.encode('utf-8')将生成b'bytes',但也会生成bytes(u'something', 'utf-8')
并且b'bytes'.decode('utf-8')似乎做同样的事情str(b'', 'utf-8')

Now my question is, why are there 2 methods that seem to do the same thing, and is either better than the other (and why?) I've been trying to find answer to this on google, but no luck.

现在我的问题是,为什么有 2 种方法似乎做同样的事情,或者比另一种更好(为什么?)我一直试图在谷歌上找到答案,但没有运气。

>>> original = '27岁少妇生孩子后变老'
>>> type(original)
<class 'str'>
>>> encoded = original.encode('utf-8')
>>> print(encoded)
b'27\xe5\xb2\x81\xe5\xb0\x91\xe5\xa6\x87\xe7\x94\x9f\xe5\xad\xa9\xe5\xad\x90\xe5\x90\x8e\xe5\x8f\x98\xe8\x80\x81'
>>> type(encoded)
<class 'bytes'>
>>> encoded2 = bytes(original, 'utf-8')
>>> print(encoded2)
b'27\xe5\xb2\x81\xe5\xb0\x91\xe5\xa6\x87\xe7\x94\x9f\xe5\xad\xa9\xe5\xad\x90\xe5\x90\x8e\xe5\x8f\x98\xe8\x80\x81'
>>> type(encoded2)
<class 'bytes'>
>>> print(encoded+encoded2)
b'27\xe5\xb2\x81\xe5\xb0\x91\xe5\xa6\x87\xe7\x94\x9f\xe5\xad\xa9\xe5\xad\x90\xe5\x90\x8e\xe5\x8f\x98\xe8\x80\x8127\xe5\xb2\x81\xe5\xb0\x91\xe5\xa6\x87\xe7\x94\x9f\xe5\xad\xa9\xe5\xad\x90\xe5\x90\x8e\xe5\x8f\x98\xe8\x80\x81'
>>> decoded = encoded.decode('utf-8')
>>> print(decoded)
27岁少妇生孩子后变老
>>> decoded2 = str(encoded2, 'utf-8')
>>> print(decoded2)
27岁少妇生孩子后变老
>>> type(decoded)
<class 'str'>
>>> type(decoded2)
<class 'str'>
>>> print(str(b'27\xe5\xb2\x81\xe5\xb0\x91\xe5\xa6\x87\xe7\x94\x9f\xe5\xad\xa9\xe5\xad\x90\xe5\x90\x8e\xe5\x8f\x98\xe8\x80\x81', 'utf-8'))
27岁少妇生孩子后变老
>>> print(b'27\xe5\xb2\x81\xe5\xb0\x91\xe5\xa6\x87\xe7\x94\x9f\xe5\xad\xa9\xe5\xad\x90\xe5\x90\x8e\xe5\x8f\x98\xe8\x80\x81'.decode('utf-8'))
27岁少妇生孩子后变老

采纳答案by Lennart Regebro

Neither is better than the other, they do exactly the same thing. However, using .encode()and .decode()is the more common way to do it. It is also compatible with Python 2.

两者都不比另一个好,它们做的事情完全一样。但是,使用.encode().decode()是更常见的方法。它还与 Python 2 兼容。

回答by pepr

To add to Lennart Regebro's answerThere is even the third way that can be used:

要添加到Lennart Regebro 的回答中,甚至可以使用第三种方法:

encoded3 = str.encode(original, 'utf-8')
print(encoded3)

Anyway, it is actually exactly the same as the first approach. It may also look that the second way is a syntactic sugar for the third approach.

无论如何,它实际上与第一种方法完全相同。看起来第二种方式也是第三种方式的语法糖。



A programming language is a means to express abstract ideas formally, to be executed by the machine. A programming language is considered good if it contains constructs that one needs. Python is a hybrid language -- i.e. more natural and more versatile than pure OO or pure procedural languages. Sometimes functions are more appropriate than the object methods, sometimes the reverse is true. It depends on mental picture of the solved problem.

编程语言是一种形式化表达抽象思想的手段,由机器执行。如果一种编程语言包含人们需要的结构,那么它就被认为是好的。Python 是一种混合语言——即比纯面向对象或纯过程语言更自然、更通用。有时函数比对象方法更合适,有时反之亦然。这取决于已解决问题的心理图景。

Anyway, the feature mentioned in the question is probably a by-product of the language implementation/design. In my opinion, this is a nice example that show the alternative thinking about technically the same thing.

无论如何,问题中提到的功能可能是语言实现/设计的副产品。在我看来,这是一个很好的例子,展示了从技术上讲同一件事的另一种思考。

In other words, calling an object method means thinking in terms "let the object gives me the wanted result". Calling a function as the alternative means "let the outer code processes the passed argument and extracts the wanted value".

换句话说,调用对象方法意味着考虑“让对象给我想要的结果”。调用函数作为替代意味着“让外部代码处理传递的参数并提取所需的值”

The first approach emphasizes the ability of the object to do the task on its own, the second approach emphasizes the ability of an separate algoritm to extract the data. Sometimes, the separate code may be that much special that it is not wise to add it as a general method to the class of the object.

第一种方法强调对象自己完成任务的能力,第二种方法强调单独的算法提取数据的能力。有时,单独的代码可能非常特殊,将其作为通用方法添加到对象的类中是不明智的。

回答by Colin Enstone

To add to add to the previous answer, there is even a fourth way that can be used

要添加到之前的答案中,甚至还有第四种方法可以使用

import codecs
encoded4 = codecs.encode(original, 'utf-8')
print(encoded4)