Search code examples
pythonhashrediskeyfield

print or set to variable - Hash key and field(value) - from redis database using python


Trying to read a hash key and field from redis Database using python. In the DB i have many numeric keys(0,1,2,etc), and each has a TIMESTAMP field with the time value (2021-08-07 11:28:07.330731)

I can read the hash key and also read the desired field, individually.

How do I get it to read\list and print out to either screen or a variable the hash key associated with the desired field value.

r = myRedis database

field = r.hget('0','TIMESTAMP') # this gives me the field "TIMESTAMP" value, from Hash with key '0' and intkeylist = r.keys() # gives me the key list

just not sure how to tie these two together, so I get all my keys associated with the fields "TIMESTAMP"(value) listed together

Desired OUTPUT: Key Field value

enter image description here

etc...


Solution

  • timestamps_log = ""    
    for x in intkeylist:
           timestamps_log += str(x + r.hget(x,'TIMESTAMP'))
    print(timestamps_log)