Python processes the chunks of json and redis hash, jsonredis
1. The data read using MySQLdb is a unicode string. If you want to write data to the redis hash, it will become
"{u'eth0_outFlow': 2.5, u'eth1_inFlow': 3.44}"
Json. loads cannot be used. unicode must be converted to str in advance:
str(eth0_outFlow)
2. Keys enclosed by single quotes are not in the standard json format.
"{'eth0_outFlow': 2.5, 'eth1_inFlow': 3.44}"
Json. loads () can be used only when it needs to be converted to a standard format ()
replace('\'', '"') => '{"eth0_outFlow": 2.5, "eth1_inFlow": 3.44}'
3. If None is written into redis, the hash is directly converted to 'None', which must be converted to 'null' to use json. loads ()
"{'eth0_outFlow': None, 'eth1_inFlow': None}"replace('\'', '"').replace("None", "null") => '{"eth0_outFlow": null, "eth1_inFlow": null}'
4. json. loads () converts the key type from str to unicode, and then writes it into redis.
"{u'eth0_outFlow': None, u'eth1_inFlow': None}"
You need to convert it into the str key before writing it into redis.
value = json.loads(cache)items = value.iteritems()value = {k.encode('utf8'): v for k, v in items}
redisCli.hmset(key, value)