用于将csv转换为yml以在azure devops中使用的Python脚本

voase2hg  于 2023-01-11  发布在  Python
关注(0)|答案(1)|浏览(121)

我们正在创建一个python脚本来将csv转换为yaml,我们将使用此yaml从azure devops创建一个管道,我创建了以下脚本:

import csv
import yaml
in_file  = open(r'C:\Users\246742\Desktop\Book2.csv', "r")
out_file = open('yaml_file.yaml', "w")
items = []

def convert_to_yaml(line, counter):
    item = {
        'task': line[0],
        'displayName': line[1],
        'inputs': {
            'ConnectedServiceName' : line[2],
            'Action' :line[3], 
            'KeyVaultName' :line[4],
            'SecretName' :line[5],
            'Secret':line[6],
            'VariableName':line[7],
            'Overwrite' : line[8]
            }
    }
    items.append(item)

try:
    reader = csv.reader(in_file)
    next(reader) # skip headers
    for counter, line in enumerate(reader):
        convert_to_yaml(line, counter)
    out_file.write( yaml.dump(items, default_flow_style=False) )

finally:
    in_file.close()
    out_file.close()

其给出输出为:

- displayName: 'Azure Key Vault: SetSecret adf-bookings'
  inputs:
    Action: abc_QA_SPN
    ConnectedServiceName: abc_QA_SPN
    KeyVaultName: SetSecret
    Overwrite: adf-booking
    Secret: adf-booking
    SecretName: $(QAkvabc)
    VariableName: $(adf-bookingst)
  task: maikvandergaag.maikvandergaag-azurekeyvault.azurekeyvault.azurekeyvaultactions@2
- displayName: 'Azure Key Vault: SetSecret adf-booking'
  inputs:
    Action: abc_QA_SPN
    ConnectedServiceName: abc_QA_SPN
    KeyVaultName: SetSecret
    Overwrite: adf-booking
    Secret: adf-booking
    SecretName: $(QAkvabc)
    VariableName: $(adf-bookingst)
  task: maikvandergaag.maikvandergaag-azurekeyvault.azurekeyvault.azurekeyvaultactions@2

但所需输出为:

trigger:
- none

variables:
- group: Devportal

pool:
  name: Ubuntu 20.04

steps:
- task: maikvandergaag.maikvandergaag-azurekeyvault.azurekeyvault.azurekeyvaultactions@2
  displayName: 'Azure Key Vault: SetSecret adf-bookings'
  inputs:
    ConnectedServiceName: 'abc_QA_SPN'
    Action: SetSecret
    KeyVaultName: '$(QAkvabc)'
    SecretName: 'adf-booking'
    Secret: '$(adf-bookingst)'
    VariableName: 'adf-booking'
    Overwrite: false
- task: maikvandergaag.maikvandergaag-azurekeyvault.azurekeyvault.azurekeyvaultactions@2
  displayName: 'Azure Key Vault: SetSecret adf-bookings'
  inputs:
    ConnectedServiceName: 'abc_QA_SPN'
    Action: SetSecret
    KeyVaultName: '$(QAkvabc)'
    SecretName: 'adf-booking'
    Secret: '$(adf-bookingst)'
    VariableName: 'adf-booking'
    Overwrite: false

在这里,字典的顺序根据字母表而改变,并且不能在值中获得引号
csv如下所示:

有人能帮我一下吗

mzsu5hc0

mzsu5hc01#

IIUC,使用f-strings

def convert_to_yaml(line, counter):
    item = {
        'task': line[0],
        'displayName': f'{line[1]}',
        'inputs': {
            'ConnectedServiceName' : f'{line[2]}',
            'Action': f'{line[3]}', 
            'KeyVaultName': f'{line[4]}',
            'SecretName': f'{line[5]}',
            'Secret': f'{line[6]}',
            'VariableName': f'{line[7]}',
            'Overwrite': f'{line[8]}'
            }
    }
    items.append(item)

试验

相关问题