gpt4 book ai didi

python - 更改 yaml 文件中的 python

转载 作者:太空宇宙 更新时间:2023-11-03 16:35:03 30 4
gpt4 key购买 nike

我必须替换 yaml 文件中的此类标签。
原来被替换的数据存储在一个列表中。
我有这个 YAML 文件

vagrantfile:    
target: local
vm:
provider:
local:
box: puphpet/ubuntu1404-x<BITS> # MEERKEUZE: 32 OF 64, STANDAARD 64
box_url: puphpet/ubuntu1404-x<BITS> # MEERKEUZE: 32 OF 64, STANDAARD 64
box_version: '0'
chosen_virtualizer: <virtualiser> # MEERKEUZE: virtualbox OF vmware, STANDAARD 64
virtualizers:
virtualbox:
modifyvm:
natdnshostresolver1: false
showgui: 0
vmware:
numvcpus: 1
parallels:
use_linked_clone: 0
check_guest_tools: 0
update_guest_tools: 0
machines:
vflm_7orc03npv15g:
id: <ID> # STRING
hostname: <HOSTNAME> # STRING
network:
private_network: <IP-ADDRESS> #lOKAAL IP, STANDARAARD 192.168.50.101
forwarded_port:
hon1d:
host: '80'
guest: '80'
memory: '<MEGABYTES>' # AANTAL, STANDAARD VEELVOUD VAN 1024
cpus: '<CORES>' # AANTAL CPUS, STANDAARD AANTAL CORES - 1, NIET HOGER DAN AANTAL CORES

我可以做什么来更改所有这些标签?

最佳答案

您应该在 YAML 级别执行此操作,只是为了确保如果您的替换强制结果字符串被引用,这实际上会发生。例如。如果您<ID>需要替换为类似 @abc 的内容YAML 文件中的实际行需要变为

id: '@abc'

而不是

id: '@abc' 

@被预定了。有几个这样的警告,因此在字符串级别上替换不是一个好主意。

你可以这样做:

import sys
import ruamel.yaml

yaml_str = """\
vagrantfile:
target: local
vm:
provider:
local:
box: puphpet/ubuntu1404-x<BITS> # MEERKEUZE: 32 OF 64, STANDAARD 64
box_url: puphpet/ubuntu1404-x<BITS> # MEERKEUZE: 32 OF 64, STANDAARD 64
box_version: '0'
chosen_virtualizer: <virtualiser> # MEERKEUZE: virtualbox OF vmware, STANDAARD 64
virtualizers:
virtualbox:
modifyvm:
natdnshostresolver1: false
showgui: 0
vmware:
numvcpus: 1
parallels:
use_linked_clone: 0
check_guest_tools: 0
update_guest_tools: 0
machines:
vflm_7orc03npv15g:
id: <ID> # STRING
hostname: <HOSTNAME> # STRING
network:
private_network: <IP-ADDRESS> #lOKAAL IP, STANDARAARD 192.168.50.101
forwarded_port:
hon1d:
host: '80'
guest: '80'
memory: '<MEGABYTES>' # AANTAL, STANDAARD VEELVOUD VAN 1024
cpus: '<CORES>' # AANTAL CPUS, STANDAARD AANTAL CORES - 1, NIET HOGER DAN AANTAL CORES
"""


def replace(data, values):
def do_one(data, values):
if isinstance(data, dict):
for k in data:
data[k] = do_one(data[k], values)
return data
elif isinstance(data, list):
for idx, elem in enumerate(data):
data[idx] = do_one(elem, values)
return data
elif isinstance(data, str):
for k in values: # full match
if data == k:
return values[k]
if '<' in data and '>' in data:
for k in values:
data = data.replace(k, str(values[k]))
return data


expanded = { ('<' + k + '>'): v for (k, v) in values.iteritems()}
do_one(data, expanded)

data = ruamel.yaml.round_trip_load(yaml_str)

replace(data, {
'BITS': 64,
'virtualiser': 'virtualbox',
'HOSTNAME': 'localhost',
'IP-ADDRESS': '192.168.0.1',
'ID': '@abc',
'MEGABYTES': 2048,
'CORES': 8,
})

ruamel.yaml.round_trip_dump(data, stream=sys.stdout, indent=4)

这会给你:

vagrantfile:
target:
vm:
provider:
local:
box: puphpet/ubuntu1404-x64 # MEERKEUZE: 32 OF 64, STANDAARD 64
box_url: puphpet/ubuntu1404-x64 # MEERKEUZE: 32 OF 64, STANDAARD 64
box_version:
chosen_virtualizer: virtualbox # MEERKEUZE: virtualbox OF vmware, STANDAARD 64
virtualizers:
virtualbox:
modifyvm:
natdnshostresolver1:
showgui:
vmware:
numvcpus:
parallels:
use_linked_clone:
check_guest_tools:
update_guest_tools:
machines:
vflm_7orc03npv15g:
id: '@abc' # STRING
hostname: localhost # STRING
network:
private_network: 192.168.0.1 #lOKAAL IP, STANDARAARD 192.168.50.101
forwarded_port:
hon1d:
host:
guest:
memory: 2048 # AANTAL, STANDAARD VEELVOUD VAN 1024
cpus: 8 # AANTAL CPUS, STANDAARD AANTAL CORES - 1, NIET HOGER DAN AANTAL CORES

请注意:

  • 评论被保留
  • cpus 的值和memory是整数,不是字符串
  • id 的值根据需要得到正确的引用。

上面使用 ruamel.yaml :免责声明 我是该包的作者。您可以对 PyYAML 执行相同的操作,但您将丢失注释,并且您需要将自己限制为 YAML 1.1 标准(从 2005 年开始),而不是 YAML 1.2 标准(从 2009 年开始)

关于python - 更改 yaml 文件中的 <TEST> python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37299882/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com