首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用人工友好语法将字符串打印到控制台日志

用人工友好语法将字符串打印到控制台日志
EN

Stack Overflow用户
提问于 2019-12-05 11:23:00
回答 1查看 57关注 0票数 0

列表对象中有以下字符串:

代码语言:javascript
复制
'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)的人友好语法,以管道控制台?例如:

代码语言:javascript
复制
repository: lld-test-helm
chart: customer-customer
version: 0.29.4

repository name: lld-test-dokcer
image: docker.io/ubuntu
tag: 18.05
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-12-05 11:50:00

您可以使用builtin eval()方法将您的字符串更改为实际的dict。当然,您需要摆脱items.find(部分和右括号)。

如果字符串总是以items.find开头(您可以这样做:

代码语言:javascript
复制
a = 'items.find({"repo": "lld-test-docker", "path": "docker.io/ubuntu/18.05", "type": "file"})'
a = a[11:-1]

或者只需使用替换:

代码语言:javascript
复制
a = a.replace('items.find(', '')[:-1]

然后,如前所述,使用eval():

代码语言:javascript
复制
a = eval(a)

现在,您可以通过一条条迭代:

代码语言:javascript
复制
for key in a:
    print(key, ' : ', a[key])

示例如何解析输出以匹配您的问题中的输出:

代码语言:javascript
复制
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:]) 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59194049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档