【发布时间】:2018-07-09 22:58:03
【问题描述】:
$ query_op=$(ssh -p 29418 myreview-server.com gerrit query --files --current-patch-set --format=JSON --commit-message change:99934 | head -n 1 | python -c "import sys, json; print json.load(sys.stdin)['currentPatchSet']['files']")
$ echo $query_op
[{u'deletions': 0, u'insertions': 11, u'type': u'ADDED', u'file': u'/COMMIT_MSG'}, {u'deletions': 0, u'insertions': 2, u'type': u'MODIFIED', u'file': u'foo/bar/baz/info.txt'}]
如何查找file 中的任何一个是否以字符串foo 开头?在上面显示的示例中,我需要做一些操作,因为有一个以foo/ 开头的文件,即foo/bar/baz/info.txt,如果在整个字典值中没有文件以foo/ 开头,那么我需要做不同的操作。
【问题讨论】:
-
为什么不在你的 Python 小块中进行解析呢?
any(x['file'].startswith('foo') for x in ...)会这样做(其中...是您当前正在打印的表达式)。 -
它确实有效,谢谢。
-
@Blckknght,您能否将其添加为解决方案,我会接受。