小编use*_*718的帖子

使用通配符搜索迭代 Redis 哈希键

我有 Redis 键和这些键的值作为哈希集(键,值对)。我正在使用 python 来检索键值。前任:

top_link:files
    key: file_path/foldername1
    value: filename1

    key: file_path/foldername2
    value: filename2

    key: test_path/foldername3
    value: filename3
Run Code Online (Sandbox Code Playgroud)

我想找出所有键名以“file_path”开头的哈希集键

我试过

all_keys = redis_connection.hscan_iter("top_link:files")
for key in all_keys:
  if key.startswith("file_path"):
    redis_connection.hget("top_link:files",key)
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来查找以“file_path”开头的所有哈希键。SCAN 似乎做了我想要实现的目标。但所有示例都显示扫描顶级键 (top_link:files),但不扫描哈希键。有什么建议么?谢谢。

python hash wildcard redis

3
推荐指数
1
解决办法
3289
查看次数

python将PST时间转换为UTC isoformat

例如:我有一个日期字符串

2018-02-17 16:15:36.519 PST
Run Code Online (Sandbox Code Playgroud)

我如何在 UTC 中转换为 isoformat,如下所示

2018-02-18T00:15:36.519Z
Run Code Online (Sandbox Code Playgroud)

我试过这个

from dateutil.parser import parse
d1='2018-02-17 16:15:36.519 PST'
print parse(d1)
it prints like this.  How do i convert it to UTC with Z at the end.
2018-02-17 16:15:36.519000-08:00
Run Code Online (Sandbox Code Playgroud)

使用 python 2.7 进行编辑

import dateutil
import pytz
from dateutil.parser import parse
d1='2018-02-17 16:15:36.519 PST'
d2=dateutil.parser.parse(d1)
d2.replace(tzinfo=pytz.utc) - d2.utcoffset()
d3=(d2.replace(tzinfo=pytz.utc) - d2.utcoffset()).isoformat()
print d3
Run Code Online (Sandbox Code Playgroud)

然后按照建议使用 Z 进行格式化

python parsing date

3
推荐指数
1
解决办法
4502
查看次数

标签 统计

python ×2

date ×1

hash ×1

parsing ×1

redis ×1

wildcard ×1