.decode('utf-8')或.decode()

时间:2016-01-20 19:04:30

标签: python-3.x utf-8 character-encoding

在Python中,您可以使用以下命令将字节解码为字符串:

s.decode('utf-8')

我刚注意到参数'utf-8'在Python 3中是可选的。
我正在编写一个需要经常编码/解码字节和字符串的库。使用s.decode()或s.decode('utf-8')更好吗?

3 个答案:

答案 0 :(得分:0)

来自Python的禅宗($ python -c 'import this'):

  

可读性很重要。


每当我使用它时,我都希望声明'utf-8'

答案 1 :(得分:0)

您应该通过常量定义特定的编码。有时你必须为diffningted servers encoding更改它。例如to' ascii'。

答案 2 :(得分:0)

Python 3的默认编码是“UTF-8”,因此.decode()将使用它。

您可以使用.decode("utf-8")

简单地使您的代码与Python 2.7兼容