如何将字符\x22转换为字符串 [英] how to convert characters like \x22 into string

查看:6854
本文介绍了如何将字符\x22转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,如下所示:

 {\\x22username\\x22:\\ \\\x229\\x22,\\x22password\\x22:\\x226\\x22,\\x22id\\x22:\\x222c8bfa56-f5d9 \\x22,\\x22FName\\x22:\\x22AnkQcAJyrqpg\\x22}

据我所知, \x22 这是一个可读的JSON,包含关键字和值的引号。

解决方案

解码从 string_escape

 >>>> import json 
>>> value ={ \\x22username\\x22:\\x229\\x22,\\x22password\\x22:\\x226\\x22,\\x22id\\ \\\x22:\\x222c8bfa56-f5d9\\x22,\\x22FName\\x22:\\x22An
'{username:9,password:6,id,
>>> value.decode :2c8bfa56-f5d9,FName:AnkQcAJyrqpg}'
>>> json.loads(value.decode('string_escape'))
{u'username' u'9',u'password':u'6',u'id':u'2c8bfa56-f5d9',u'FName':u'AnkQcAJyrqpg'}
pre>

I have a string that looks like this:

"{\\x22username\\x22:\\x229\\x22,\\x22password\\x22:\\x226\\x22,\\x22id\\x22:\\x222c8bfa56-f5d9\\x22, \\x22FName\\x22:\\x22AnkQcAJyrqpg\\x22}"

as far as I understand \x22 is ". So how could I convert this into a readable JSON with quotes around keys and values?

解决方案

Decode from string_escape:

>>> import json
>>> value = "{\\x22username\\x22:\\x229\\x22,\\x22password\\x22:\\x226\\x22,\\x22id\\x22:\\x222c8bfa56-f5d9\\x22, \\x22FName\\x22:\\x22AnkQcAJyrqpg\\x22}"
>>> value.decode('string_escape')
'{"username":"9","password":"6","id":"2c8bfa56-f5d9", "FName":"AnkQcAJyrqpg"}'
>>> json.loads(value.decode('string_escape'))
{u'username': u'9', u'password': u'6', u'id': u'2c8bfa56-f5d9', u'FName': u'AnkQcAJyrqpg'}

这篇关于如何将字符\x22转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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