我收到错误,例如
UnicodeEncodeError('ascii', u'\x01\xff \xfeJ a z z', 1, 2, 'ordinal not in range(128)'
我也得到了诸如
u'\x17\x01\xff \xfeA r t B l a k e y'
我将 \x01\xff\xfe 识别为 BOM,但如何将它们转换为明显的输出(Jazz 和 Art Blakey)?
这些来自读取音乐文件标签的程序。
我尝试了各种编码,例如 s.encode('utf8'),以及各种解码后跟编码,但均未成功。
按照要求:
from hsaudiotag import auto
inf = 'test.mp3'
song = auto.File(inf)
print song.album, song.artist, song.title, song.genre
> Traceback (most recent call last): File "audio2.py", line 4, in
> <module>
> print song.album, song.artist, song.title, song.genre File "C:\program files\python27\lib\encodings\cp437.py", line 12, in encode
> return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\xfe' in
> position 4 : character maps to <undefined>
如果我将打印语句更改为
with open('x', 'wb') as f:
f.write(song.genre)
我明白了
Traceback (most recent call last):
File "audio2.py", line 6, in <module>
f.write(song.genre)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xff' in position 1:
ordinal not in range(128)