目前我有:
>> class_name = 'AEROSPC\xc2\xa01A'
>> print(class)
>> AEROSPC 1A
>> 'AEROSPC 1A' == class_name
>> False
如何将class_name
转换为'AEROSPC 1A'?谢谢!
答案 0 :(得分:3)
转换时会出现有趣的错误,我首先转换为utf8:
my_utf8 = 'AEROSPC\xc2\xa01A'.decode('utf8', 'ignore')
my_utf8
返回:
u'AEROSPC\xa01A'
然后我将字符串规范化,\ xa0是一个不间断的空格。
import unicodedata
my_normed_utf8 = unicodedata.normalize('NFKC', my_utf8)
print my_normed_utf8
打印:
AEROSPC 1A
然后我可以将其转换回ASCII字符串:
my_str = str(my_normed_utf8)
print my_str
打印:
AEROSPC 1A