python - JMESPath 日期过滤
问题描述
我正在尝试将 Ansible 脚本转换为 Python AWS lambda 函数。在我的 Python 脚本中,我使用该jmespath
库按日期过滤,该日期以 ISO 8601 格式的字符串形式给出。
我的过滤器在我的 Ansible 脚本中工作,并且还使用JMESPath 网站工具。我无法弄清楚为什么我的 Python 版本不起作用;Ansible 是用 Python 编写的,所以我希望它能够以同样的方式工作。
这在 Ansible 中有效:
- name: parse groups
debug:
msg: "{{ results_gitlabGroupsProjects | to_json | from_json | json_query(projects_query) }}"
vars:
projects_query: "json[?last_activity_at > `{{gitlab_date}}`].{name: name, id: id, last_activity_at: last_activity_at }"
register: gitlabGroupsProjects2
当我尝试在 Python 中做同样的事情时,我得到一个空列表[]
:
compareTime="2020-01-15T17:55:3S.390Z"
plist2 = jmespath.search('[?last_activity_at > `str(compareTime)`]', project_data )
with open('plist2.json', 'w') as json_file:
json.dump(plist2, json_file)
示例 JSON 数据:
[
{
"name": "test",
"id": 16340975,
"last_activity_at": "2020-01-15T20:12:49.775Z"
},
{
"name": "test1",
"id": 11111111,
"last_activity_at": "2020-01-15T15:57:29.670Z"
},
{
"name": "test2",
"id": 222222,
"last_activity_at": "2020-01-15T23:08:22.313Z"
},
{
"name": "test3",
"id": 133333,
"last_activity_at": "2020-01-15T22:28:42.628Z"
},
{
"name": "test4",
"id": 444444,
"last_activity_at": "2020-01-14T02:20:47.496Z"
},
{
"name": "test5",
"id": 555555,
"last_activity_at": "2020-01-13T04:54:18.353Z"
},
{
"name": "test6",
"id": 66666666,
"last_activity_at": "2020-01-12T07:12:05.858Z"
},
{
"name": "test7",
"id": 7777777,
"last_activity_at": "2020-01-10T20:52:32.269Z"
}
]
使用 Ansible,在 JMESPath 网站上我得到以下输出:
[
{
"name": "test",
"id": 16340975,
"last_activity_at": "2020-01-15T20:12:49.775Z"
},
{
"name": "test2",
"id": 222222,
"last_activity_at": "2020-01-15T23:08:22.313Z"
},
{
"name": "test3",
"id": 133333,
"last_activity_at": "2020-01-15T22:28:42.628Z"
}
]
解决方案
您不能只将str(compareTime)
表达式作为字符串文字放入并让 Python 了解您想要替换的内容。
在字符串中
'[?last_activity_at > `str(compareTime)`]'
没有什么是动态的;这些字符str(compareTime)
对 Python 没有特殊含义。
在Ansible中,您使用了特殊语法 ,{{gitlab_date}}
来告诉 Ansible 以不同的方式处理字符串,并且在将变量的值gitlab_date
用作 JMESPath 查询之前,该变量的值被放入字符串中。
在 Python 中,您需要类似的东西。例如,您可以使用格式化的字符串文字或f-string:
plist2 = jmespath.search(f"[?last_activity_at > `{compareTime}`]", project_data)
before 字符串文字告诉 Python在大括号f
之间查找任何表达式,因此在字符串的该点处插入到位。这很像 Ansible 语法。{...}
compareTime
以上产生了您的预期输出:
>>> jmespath.search(f"[?last_activity_at > `{compareTime}`]", project_data)
[{'name': 'test', 'id': 16340975, 'last_activity_at': '2020-01-15T20:12:49.775Z'}, {'name': 'test2', 'id': 222222, 'last_activity_at': '2020-01-15T23:08:22.313Z'}, {'name': 'test3', 'id': 133333, 'last_activity_at': '2020-01-15T22:28:42.628Z'}]
>>> from pprint import pprint
>>> pprint(_)
[{'id': 16340975,
'last_activity_at': '2020-01-15T20:12:49.775Z',
'name': 'test'},
{'id': 222222,
'last_activity_at': '2020-01-15T23:08:22.313Z',
'name': 'test2'},
{'id': 133333,
'last_activity_at': '2020-01-15T22:28:42.628Z',
'name': 'test3'}]
推荐阅读
- html - 如何在一行中写两个文本
- 标签之间的间距相等 CSS
- windows - 使用 electron-builder 将电子应用程序打包到单个 .exe 安装程序中
- javascript - React Native Firestore 删除数组中的对象
- python - 如何解决“意外的关键字参数”错误
- android - 运行示例 Android Studio 应用程序时出现问题
- windows - 我的路径中的哪个目录导致 Qt 认为我使用的是 Q_OS_LINUX 而不是 Q_OS_WIN?
- python - tkinter __init__ 的问题
- amazon-sns - 删除 SNS 平台应用程序是否会删除所有已注册的端点?
- c - 在具有 64 位 Linux 的 Raspberry Pi 4 上用 C 语言用汇编语言添加两个双精度浮点数
- python - Python将jpg插入Word文档中的特定位置