用随机数替换 YAML 文件中的数字的问题

Problems with replacing numbers in YAML file with random numbers

我正在尝试使用 ruamel.yaml 编辑 YAML 文件。我想让每个 X、Y、Z 都得到一个随机数。此外,我使用此代码编辑我的 YAML:

import sys
from random import randrange
from pathlib import Path
import ruamel.yaml

in_file = Path('input.yaml')
out_file = Path('output.yaml')

def randfloatstr():
    # this gives you max 4 digits before the comma and 5 digits after
    x = str(randrange(0, 1000000000))
    return x[:-5] + ',' + x[-5:]
    
yaml = ruamel.yaml.YAML()
data = yaml.load(in_file)
for v in data.values():
    for k in v:
        v[k] = randfloatstr()

yaml.dump(data, out_file)
sys.stdout.write(out_file.read_text())

我要编辑的文件是:

Version: 3
IsBigEndian: False
SupportPaths: False
HasReferenceNodes: False
root:
  - !h 1: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 2: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 3: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 4: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 5: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 6: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    UniqueId: !l 17
  - !h 1: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 2: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 3: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 4: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 5: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 6: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    UniqueId: !l 22
  - !h 1: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 2: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 3: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 4: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 5: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 6: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    UniqueId: !l 18
  - !h 1: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 2: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 3: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 4: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 5: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}
    !h 6: {X: '-950,00000', Y: '1500,00000', Z: '150,00000'}

文件较长,但与 YAML 文件的第一行具有相同的文件结构。 我在做什么假的? 有关代码的更多信息,请转到 .

您的输入与另一个问题的输入类似,但在结构上,开始 从根本上来说,完全不同:

  • 另一个 YAML 位于映射的根部,该映射的值又是映射
  • 这也有 YAML 在根有一个映射,但是它的值在之后是不同的类型 加载(整数、布尔值、列表)

其实只是序列的元素对于根级别有点值 对应于您在另一个问题中的结构。但是因为这些元素是映射 包含额外的键,代码需要考虑到这一点:

import sys
from random import randrange
from pathlib import Path
import ruamel.yaml

in_file = Path('input.yaml')
out_file = Path('output.yaml')

def randfloatstr():
    # this gives you max 4 digits before the comma and 5 digits after
    x = str(randrange(0, 1000000000))
    return x[:-5] + ',' + x[-5:]
    
yaml = ruamel.yaml.YAML()
data = yaml.load(in_file)
for elem in data['root']:  # dig down in the data structure 
    for v in elem.values():
        if not isinstance(v, dict):  # this skips key UniqueId
            continue
        for k in v:
            v[k] = randfloatstr()

yaml.dump(data, out_file)
sys.stdout.write(out_file.read_text())

给出:

Version: 3
IsBigEndian: false
SupportPaths: false
HasReferenceNodes: false
root:
- !h 1: {X: '7204,21460', Y: '3605,58375', Z: '7812,50654'}
  !h 2: {X: '4982,60453', Y: '810,27432', Z: '6550,85588'}
  !h 3: {X: '5964,42497', Y: '3804,28963', Z: '1917,87928'}
  !h 4: {X: '2987,43168', Y: '6306,05597', Z: '9357,89699'}
  !h 5: {X: '6460,45753', Y: '4387,88509', Z: '1981,16380'}
  !h 6: {X: '1406,47354', Y: '1249,25896', Z: '3995,81558'}
  UniqueId: !l 17
- !h 1: {X: '8038,96607', Y: '1296,53796', Z: '2704,07560'}
  !h 2: {X: '8040,12804', Y: '7553,00224', Z: '5576,42482'}
  !h 3: {X: '2670,92269', Y: '2571,28860', Z: '4504,94541'}
  !h 4: {X: '850,78946', Y: '2673,26048', Z: '2955,52854'}
  !h 5: {X: '2583,01825', Y: '2088,59196', Z: '8956,01447'}
  !h 6: {X: '1501,98382', Y: '8540,05252', Z: '4855,81919'}
  UniqueId: !l 22
- !h 1: {X: '5805,88822', Y: '7341,72832', Z: '8619,82208'}
  !h 2: {X: '2948,69725', Y: '4142,56070', Z: '6012,64838'}
  !h 3: {X: '5575,74616', Y: '8735,55800', Z: '9382,28785'}
  !h 4: {X: '5222,79486', Y: '8640,50950', Z: '4615,36488'}
  !h 5: {X: '2498,86001', Y: '7544,10835', Z: '1513,44245'}
  !h 6: {X: '5999,18886', Y: '3900,92487', Z: '9941,12862'}
  UniqueId: !l 18
- !h 1: {X: '538,79550', Y: '2807,87510', Z: '679,19232'}
  !h 2: {X: '4333,72089', Y: '7161,19597', Z: '5714,09926'}
  !h 3: {X: '9425,87201', Y: '891,11245', Z: '2506,71649'}
  !h 4: {X: '9638,40660', Y: '1905,53429', Z: '8531,39175'}
  !h 5: {X: '229,66477', Y: '6680,15910', Z: '9408,97562'}
  !h 6: {X: '5219,31438', Y: '503,59622', Z: '5455,60620'}