Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x .decode(';utf-8';)或.decode()_Python 3.x_Utf 8_Character Encoding - Fatal编程技术网

Python 3.x .decode(';utf-8';)或.decode()

Python 3.x .decode(';utf-8';)或.decode(),python-3.x,utf-8,character-encoding,Python 3.x,Utf 8,Character Encoding,在Python中,可以使用以下命令将字节解码为字符串: s.decode('utf-8') 我刚刚注意到参数“utf-8”在Python 3中是可选的。我正在编写一个库,需要经常对字节和字符串进行编码/解码。使用s.decode()或s.decode('utf-8')更好吗?来自Python的Zen($Python-c'import this'): 可读性很重要 每当我使用它时,我宁愿声明'utf-8'。来自Python的Zen($Python-c'import this'): 可读性很重要

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

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

来自Python的Zen(
$Python-c'import this'
):

可读性很重要


每当我使用它时,我宁愿声明
'utf-8'

来自Python的Zen(
$Python-c'import this'
):

可读性很重要



每当我使用它时,我更喜欢声明
'utf-8'

您应该定义特定的编码,即使是常量。有时您必须将其更改为衍射服务器编码。例如,更改为“ascii”。

您应该定义特定的编码,即使是常数。有时您必须将其更改为不同的服务器编码。例如,更改为“ascii”。

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


只需使用
.decode(“utf-8”)
就可以使代码与Python 2.7兼容。Python 3的默认编码是“utf-8”,因此
.decode()
将使用该编码

只需使用
.decode(“utf-8”)