如何在python中unescape unicode转义的字符串? [英] How do I unescape a unicode escaped string in python?

查看:314
本文介绍了如何在python中unescape unicode转义的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.4



我有一个unicode转义字符串:

 >我想把这个字符串转换成unicode unescaped的版本。 'blah-dude' 



我该怎么做?

解决方案

将其编码为 bytes (使用任何编解码器,utf-8可能工作)然后使用 unicode-escape

  s ='blah\\\x2x2Ddude'

s.encode()。decode('unicode-escape')
输出[133]:'blah-dude'


Python 3.4

I have a unicode escaped string:

> str = 'blah\\x2Ddude'

I want to convert this string into the unicode unescaped version 'blah-dude'

How do I do this?

解决方案

Encode it to bytes (using whatever codec, utf-8 probably works) then decode it using unicode-escape:

s = 'blah\\x2Ddude'

s.encode().decode('unicode-escape')
Out[133]: 'blah-dude'

这篇关于如何在python中unescape unicode转义的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆