I got below stated output when I queried hgetall to redis from a python3 script.
data = {
b'category': b'0',
b'title': b'1',
b'display': b'1,2',
b'type': b'1',
b'secret': b'this_is_a_salt_key',
b'client': b'5'}
it was of type dict.
When I tried to get "category" like
>>> data['category']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'category'
Upon reading I tried this way
import ast
>>> ast.literal_eval(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/ast.py", line 84, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python3.4/ast.py", line 83, in _convert
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: {b'category': b'0', b'title': b'1', b'display': b'1,2', b'type': b'1', b'secret': b'this_is_a_salt_key', b'client': b'5'}
also tried using json.dumps. but could not understand the real problem.
Please help me to parse the output and get the desired result.
this was my desired output.. thanks to all.
This is not JSON, so there is no point trying to parse it. It is a dictionary, which just happens to have keys which are byte strings. So you simply need to use byte strings to access the values:
You have to add the
b
in front of the key value since it is a byte string:If you want to turn the byte strings into normal strings you could do: