通过 redis-py 将 postgresql timestamptz 作为分数存储到 redis 排序集:DataError

Storing postgresql timestamptz as score to redis sorted set via redis-py : DataError

我正在使用 redis-py 将 postgresql timestamptz 存储到 redis 排序集。

时间戳作为分数,数据作为价值。

我需要降序排列 order.But 我无法将数据插入 redis.I 不知道如何转换成 redis 支持的格式。 这是代码:

  cursor.execute("select current_timestamp;");
  timestamp_raw=cursor.fetchone()
  redis_client.zadd("stream",{data:timestamp_raw})

错误如下

.....\AppData\Local\Programs\Python\Python38-32\lib\site-packages\redis\connection.py", line 117, in encode
raise DataError("Invalid input of type: '%s'. Convert to a "redis.exceptions.DataError: Invalid input of type: 'tuple'. Convert to a byte, string or number first.

如何重新爱它?

在 Psycopg2 中,timestamptz 作为日期时间和 FixedOffsetTimezone 的元组返回。

您需要将元组转换为浮点数,以便将其用作分数。

Converting datetime.date to UTC timestamp in Python