简体   繁体   English

如何将unicode字符串转换为相应的ascii字符串?

[英]How to convert a unicode string to the corresponding ascii string?

This code: 这段代码:

print u'S\xe9parateur'
str(u'S\xe9parateur')

throws the error: 引发错误:

Séparateur
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)

I used the function str because I want to convert the unicode string u'S\\xe9parateur' to the corresponding ascii string, ie u'S\\xe9parateur' --> 'S\\xc3\\xa9parateur' 我使用函数str是因为我想将Unicode字符串u'S\\xe9parateur'转换为相应的ascii字符串,即u'S\\xe9parateur' -> 'S\\xc3\\xa9parateur'

That's not a corresponding ASCII string, it's a UTF-8 string. 这不是相应的ASCII字符串,而是UTF-8字符串。 ASCII, definitionally, cannot represent the whole of the Unicode range; 从定义上讲,ASCII不能代表整个Unicode范围。 UTF-8 can. UTF-8可以。

To perform the conversion, use the .encode method of unicode objects: 要执行转换,请使用unicode对象的.encode方法:

u'S\xe9parateur'.encode('utf-8')

produces 'S\\xc3\\xa9parateur' . 产生'S\\xc3\\xa9parateur'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM