LA_*_*LA_ 1 python unicode encoding utf-8 eyed3
ID3_V1仅支持latin1编码.为了用俄语字符写V1标签,使用cp1251编码.我想将数据从V2标签(unicode)复制到V1标签.我使用eyeD3用法获得带有以下代码的V2标签:
tag.link(mp3path, v=eyeD3.ID3_V2)
mp3album_v2 = tag.getAlbum()
...
tag.link(mp3path, v=eyeD3.ID3_V1)
tag.setTextEncoding(eyeD3.LATIN1_ENCODING)
tag.setAlbum(mp3album_v2.encode('cp1251')) # ???
tag.update()
Run Code Online (Sandbox Code Playgroud)
返回以下内容:
>>> print mp3album_v2
???? ? ????? ??????
>>> print type(mp3album_v2)
<type 'unicode'>
>>> print repr(mp3album_v2)
u'\u0416\u0438\u0442\u044c \u0432 \u0442\u0432\u043e\u0435\u0439 \u0433\u043e\u043b\u043e\u0432\u0435'
Run Code Online (Sandbox Code Playgroud)
看起来像setAlbum期望utf-8字符串(?):
def setAlbum(self, a):
self.setTextFrame(ALBUM_FID, self.strToUnicode(a));
def strToUnicode(self, s):
t = type(s);
if t != unicode and t == str:
s = unicode(s, eyeD3.LOCAL_ENCODING);
elif t != unicode and t != str:
raise TagException("Wrong type passed to strToUnicode: %s" % str(t));
return s;
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试这样做tag.setAlbum(mp3album_v2.encode('cp1251').encode('utf-8')),那么我会收到错误UnicodeDecodeError: 'utf8' codec can't decode byte 0xc6 in position 0: invalid continuation byte
ID3v1不能可靠地包含任何非ASCII字符.您可以将cp1251编码的字节写入ID3v1标记,但它们只会在俄语语言环境操作系统安装上呈现为Cyrillic,甚至不会在所有应用程序上呈现.
EyeD3在内部处理Unicode字符串,并且任意选择使用latin1(也称为ISO-8859-1)作为ID3v1标记的编码.这可能不是一个好的选择,因为latin1它永远不是Windows框中默认的特定于语言环境的编码(对于西欧来说,它实际上cp1252是相似但不相同的).
但是,这种编码选择的属性是它中的每个字节映射到具有相同代码点编号的Unicode字符.您可以通过创建一个包含字符的Unicode字符串来利用这一点,这些字符在编码latin1时最终将成为除了以外的编码中所选字符串的字节编码latin1.
album_name = u'???? ? ????? ??????'
mangled_name = album_name.encode('cp1251').decode('latin1')
tag.setAlbum(mangled_name) # will encode as latin1, resulting in cp1251 bytes
Run Code Online (Sandbox Code Playgroud)
这是一个可怕的黑客,可疑的好处,也是你应该避免使用ID3v1的原因之一.
| 归档时间: |
|
| 查看次数: |
1403 次 |
| 最近记录: |