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