Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
转换Python';s内部str打印等效文件_Python_String_Python 2.7_Unicode_Utf8 Decode - Fatal编程技术网

转换Python';s内部str打印等效文件

转换Python';s内部str打印等效文件,python,string,python-2.7,unicode,utf8-decode,Python,String,Python 2.7,Unicode,Utf8 Decode,目前我有: >> class_name = 'AEROSPC\xc2\xa01A' >> print(class) >> AEROSPC 1A >> 'AEROSPC 1A' == class_name >> False 如何将class_name转换为“AEROSPC 1A”?谢谢 转换为Unicode 转换时会出现有趣的错误,我首先转换为utf8: my_utf8 = 'AEROSPC\xc2\xa01A'.decode

目前我有:

 >> class_name = 'AEROSPC\xc2\xa01A'
 >> print(class)
 >> AEROSPC 1A
 >> 'AEROSPC 1A' == class_name
 >> False
如何将
class_name
转换为“AEROSPC 1A”?谢谢

转换为Unicode 转换时会出现有趣的错误,我首先转换为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
AEROSPC 1A
转换回字符串 然后我可以将其转换回ASCII字符串:

my_str = str(my_normed_utf8)
print my_str
印刷品:

AEROSPC 1A
AEROSPC 1A

我回答你的问题了吗?这就是我想要的,谢谢!