Redis Chinese storage successfully, read data write file garbled problem

Source: Internet
Author: User

Redis is needed in the near future, but it's been a very long time in coding this issue.
Requirement: Each day a process documents the Chinese file into Redis (not regularly updated), and several other processes read the information in Redis and process the data results.  Redis module used: Inbound normal, read data successfully, write to GBK encoded file exception. Connect to Redis using the following parameters:
Client = Redis. Strictredis (host= ' localhost ', port=6379, db=0, password= "* * *") learned from the StackOverflow: It is best to pass in a str type value to Redis, Instead of Unicode, Redis will use the SET command directly, set your value to Utf-8 format, and when you use the Get method to get data, Redis itself does not care about the type of your value data, and returns you with a str type of value. Therefore, the type of value that you store is key, and the main body is now in the source code of Redis-py:

        "" "        Encode the value so it's identical to what we'll        read off the connection" ""        if Self.decode_respo Nses and isinstance (value, bytes):                                                                   value = Value.decode (self.encoding, self.encoding_errors)        elif not Self.decode_responses and Isinstance (value, Unicode):            value = Value.encode (self.encoding, self.encoding_errors)        return value

Workaround: When using the Redis API to connect to a database:

Class  Redis. Strictredis (host= ' localhost ', port=6379, db=0, Password=none, Socket_timeout=none,connection_pool=none, charset= ' GBK ', errors= ' strict ', decode_responses=true, Unix_socket_path=none)

By setting the parameters above, the coding problem is overcome.


If someone has a better explanation and solution, please share!



Redis Chinese storage successfully, read data write file garbled problem

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.