导读热词使用Python如何操作Redis呢?下面用实例来说明用Python读写Redis数据库。
比如,我们插入一条数据,如下:
import redis
class Database:
def __init__(self):
self.host = 'localhost'
self.port = 6379
def write(self,website,city,year,month,day,deal_number):
try:
key = '_'.join([website,str(year),str(month),str(day)])
val = deal_number
r = redis.StrictRedis(host=self.host,port=self.port)
r.set(key,val)
except Exception,exception:
print exception
def read(self,day):
try:
key = '_'.join([website,str(day)])
r = redis.StrictRedis(host=self.host,port=self.port)
value = r.get(key)
print value
return value
except Exception,exception:
print exception
if __name__ == '__main__':</