将u'\ xhh'之类的unicode转换为string

时间:2018-03-17 09:17:26

标签: python python-2.7 python-unicode

我正在使用python2模块pytaglib来编辑mp3标签。一个mp3的艺术家

u'\xc3\xd7\xbd\xf2\xd0\xfe\x8e\x9f'

当我打印unicode时,会引发错误

UnicodeEncodeError: 'gbk' codec can't encode character u'\xc3' in position 0: illegal multibyte sequence

错误后我在下面添加第3-5行

import taglib
song = taglib.File("D:/song.mp3")
for i in song.tags.iteritems():
    print i
print
for k,v in song.tags.iteritems():
    print k+":",
    print v[0]

显示

(u'ALBUM', [u'Lemon'])
(u'TITLE', [u'Lemon'])
(u'ARTIST', [u'\xc3\xd7\xbd\xf2\xd0\xfe\x8e\x9f'])
(u'ENCODING', [u'Lavf57.71.100'])
(u'ALBUMARTIST', [u'\xc3\xd7\xbd\xf2\xd0\xfe\x8e\x9f'])
(u'TRACKNUMBER', [u'1'])

ALBUM: Lemon
TITLE: Lemon
ARTIST: Traceback (most recent call last):
  File "D:/mp3tag.py", line 8, in <module>
    print v[0]
UnicodeEncodeError: 'gbk' codec can't encode character u'\xc3' in position 0: illegal multibyte sequence

然后我尝试了下面的代码

>print '\xc3\xd7\xbd\xf2\xd0\xfe\x8e\x9f'
米津玄師

我必须以这种方式打印unicode

s = u'\xc3\xd7\xbd\xf2\xd0\xfe\x8e\x9f'
print eval('"'+repr(s)[2:-1]+'"')

有没有更好的方法将u'\xc3\xd7\xbd\xf2\xd0\xfe\x8e\x9f'转换为'\xc3\xd7\xbd\xf2\xd0\xfe\x8e\x9f'

0 个答案:

没有答案