如何将 x22 之类的字符转换为字符串? [英] How to convert characters like x22 into a string?
本文介绍了如何将 x22 之类的字符转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个看起来像这样的字符串:
"{\x22username\x22:\x229\x22,\x22password\x22:\x226\x22,\x22id\x22:\x222c8bfa56-f5d9\x22, \x22FName\x22:\x22AnkQcAJyrqpg\x22}"
据我所知 x22
是 "
.那么我怎么能把它转换成可读的 JSON,在键和值周围加上引号?
解决方案
Decode from string_escape
:
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屋!
查看全文