列表对象中有以下字符串:
'items.find({"repo": "lld-test-helm", "path": "customer-customer", "name": "customer-customer-0.29.4.tgz", "type": "file"})'
'items.find({"repo": "lld-test-docker", "path": "docker.io/ubuntu/18.05", "type": "file"})'请您建议如何操作和打印它(使用python 3)的人友好语法,以管道控制台?例如:
repository: lld-test-helm
chart: customer-customer
version: 0.29.4
repository name: lld-test-dokcer
image: docker.io/ubuntu
tag: 18.05发布于 2019-12-05 11:50:00
您可以使用builtin eval()方法将您的字符串更改为实际的dict。当然,您需要摆脱items.find(部分和右括号)。
如果字符串总是以items.find开头(您可以这样做:
a = 'items.find({"repo": "lld-test-docker", "path": "docker.io/ubuntu/18.05", "type": "file"})'
a = a[11:-1]或者只需使用替换:
a = a.replace('items.find(', '')[:-1]然后,如前所述,使用eval():
a = eval(a)现在,您可以通过一条条迭代:
for key in a:
print(key, ' : ', a[key])示例如何解析输出以匹配您的问题中的输出:
b = {"repo": "lld-test-docker", "path": "docker.io/ubuntu/18.05", "type": "file"}
for item in b:
if item == "repo":
print('repository : ', b[item])
if item == "path":
if "ubuntu" in b[item]:
separator = len('ubuntu')+b[item].find('ubuntu')
print('image : ', b[item][:separator])
print('tag : ', b[item][separator+1:]) https://stackoverflow.com/questions/59194049
复制相似问题