【发布时间】:2021-10-13 07:13:19
【问题描述】:
我有一个包含以下行的 Dockerfile:
LABEL "com.datadoghq.ad.logs"='[{"source": "mysource", "name": "myservicename"}]'
我有一个 python 脚本,我想读取这个 LABEL 值(“LABEL”之后的所有内容并将其添加到 yaml 文件中。这是我的脚本:
#!/usr/bin/python3
import yaml
def _extract_dockerfile_labels():
labels = []
with open("testing/dockerfile") as dockerfile:
for line in dockerfile.readlines():
if line.startswith('LABEL'):
label_val = line.replace("LABEL ","", 1)
labels.append(label_val.rstrip()). # rstrip() gets rid of the newline
with open("output.yml", "w+") as out_file:
yaml.dump(labels, out_file, default_flow_style=False)
_extract_dockerfile_labels()
输出文件如下所示:
- '"com.datadoghq.ad.logs"=''[{"source": "mysource", "name": "myservicename"}]'''
我需要的是:
- "com.datadoghq.ad.logs"='[{"source": "mysource", "name": "myservicename"}]'
如何在不添加额外引号的情况下完成这项工作?
【问题讨论】: