Python processes the chunks of json and redis hash, jsonredis

Source: Internet
Author: User

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)

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.