该文件可以具有cp1251
和utf-8
编码。我应该:
- 删除 id3 v2 标签
- 将 id3 v1 标签的编码更改为
iso-8859-5
我执行以下操作:
def getTagStr (tagUnicStr):
# gets the 1byte 8bits string, as written in the tag, from the unicode, returned by tag.get*
# taken from tag2utf-0.16 by Kopats Andrei
ls = []
for i in range(0,len(tagUnicStr)):
if (ord(tagUnicStr[i]) in range(256)):
ls.append(chr(ord(tagUnicStr[i])))
else:
ls.append(tagUnicStr[i])
Str8 = ''.join(ls)
return Str8
trackInfo = eyeD3.Mp3AudioFile(path)
tag = trackInfo.getTag()
tag.link(path)
mp3artist = tag.getArtist() # contains Russian characters
mp3artist = getTagStr(mp3artist)
mp3encoding = 'utf-8'
try:
# pseudo utf-8 encoding
mp3artist = mp3artist.decode('utf-8')
except UnicodeDecodeError, err:
# cp1251
mp3artist = mp3artist.decode('cp1251')
mp3encoding = 'cp1251'
except UnicodeEncodeError, err:
# utf-8
pass
tag.setArtist(mp3artist.encode('iso-8859-5'))
tag.update()
它在最后一行失败并出现以下错误:
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 1-5: ordinal not in range(256)
我的代码有什么问题?