Home  >  Article  >  Backend Development  >  python convert escaped string

python convert escaped string

巴扎黑
巴扎黑Original
2016-12-03 10:55:271007browse

python转换已转义的字符串

有时我们可能会获取得以下这样的字符串:

Python代码  

>>> a = '{\\"name\\":\\"michael\\"}'  
>>> print a  
{\"name\":\"michael\"}

 

那么该如何将其转换为一个字典呢?

可以用以下的方法:

Python代码  

>>> type(json.loads('“' + a + '”'))  
<type &#39;unicode&#39;>  
>>> type(json.loads(json.loads(&#39;“&#39; + a + &#39;”&#39;)))  
<type &#39;dict&#39;>

第一次json.loads是将里面的\"这样的字符串转为"(只有一个双引号),第二次再将其转为一个字典,记得不要漏掉前面先加双引号。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn