python convert escaped string

巴扎黑
Release: 2016-12-03 10:55:27
Original
1050 people have browsed it

python转换已转义的字符串

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

Python代码  

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

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

可以用以下的方法:

Python代码

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

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


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!