您的位置:首页 > 编程语言 > Python开发

Python的ASCII, UTF-8 相互转换

2017-08-16 19:28 381 查看
如果原来的string是utf-8编码的,想转成ascii编码,那么:

例如,
str=“你好”


utf-8编码: ‘\xe4\xbd\xa0\xe5\xa5\xbd’

转成 ascii编码: u’\u4f60\u597d’

方法是:str.decode(‘utf-8’)

反之,如果原来的string是ascii编码的,想转成utf-8编码,那么:

例如,
str=“你好”


ascii编码: u’\u4f60\u597d’

转成 utf-8编码:’\xe4\xbd\xa0\xe5\xa5\xbd’

方法是:str.decode(‘ascii’)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: