Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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.3 utf-8 id3字符串解码_Python_Python 3.x_Utf 8 - Fatal编程技术网

Python-3.3 utf-8 id3字符串解码

Python-3.3 utf-8 id3字符串解码,python,python-3.x,utf-8,Python,Python 3.x,Utf 8,我很难理解这个python代码出了什么问题: import chardet from mutagen.mp3 import MP3 id3 = MP3("./test.mp3") artist_name_byte = bytes(id3['TPE1']) print( artist_name_byte) encoding = chardet.detect(artist_name_byte) print(encoding['encoding']) artist_name_string =

我很难理解这个python代码出了什么问题:

import chardet
from mutagen.mp3 import MP3

id3 = MP3("./test.mp3")

artist_name_byte = bytes(id3['TPE1'])
print( artist_name_byte)

encoding = chardet.detect(artist_name_byte)
print(encoding['encoding'])

artist_name_string = artist_name_byte.decode('utf-8')
print(artist_name_string)
输出为:

b'H\xc3\xa9l\xc3\xa8ne Grimaud'
utf-8
[Decode error - output not utf-8]

为什么会出现解码错误?

此处没有解码错误:

>>> b'H\xc3\xa9l\xc3\xa8ne Grimaud'.decode('utf8')
'Hélène Grimaud'
很可能是打印unicode字符串时出现异常。您没有显示回溯,但需要注意抛出回溯的行以及异常的确切名称

例如,
print()
行将是回溯中的最后一行,而不是
.decode()
行,例外情况将是
UnicodeEncodeError
,而不是
UnicodeEncodeError


确保您的控制台、终端或IDE设置正确,并且能够处理拉丁语-1代码点。如果Python告诉您,
'ascii'编解码器不能对字符进行编码…
那么控制台很可能不会告诉Python什么编解码器是可以接受的,例如。

代码对我有效(解码为
'Hélène Grimaud'
),您确定没有
UnicodeCodeException
(编码)错误?您很可能在打印时出现异常,而不是解码艺术家名称。你在哪个站台?您的控制台、终端或IDE是什么?能否向我们展示完整的回溯?
[解码错误-输出不是utf-8]
行是您的解释,这不足以让我们看到您的实际异常情况。@MartijnPieters您是对的。。。我是从Sublimitext启动代码的。在windows命令提示符下运行它一切正常!非常感谢。如果您将其作为答案写入,我将接受。将编辑器配置为设置
pythonionecoding=utf-8
,您还可以使用utf-8编码打开输出文本文件,并将结果写入文件,而不是使用
打印
。任何支持UTF-8的体面编辑器都会向您显示文本,即使在console无法显示文本的情况下也是如此。