Home > Software engineering >  Problems with replacing numbers in YAML file with random numbers
Problems with replacing numbers in YAML file with random numbers

Time:04-24

I'm trying to edit an YAML file using ruamel.yaml. I want to make that every X, Y, Z gets a random number. Furthermore, I used this code to edit my 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())

The file I'm trying to edit is:

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'}

The file is longer, but has the same file structure as the first lines of the YAML file. What I am making false? For more informations for the code go to this question.

CodePudding user response:

Your input is similar to that of the other question, but structurally, starting from the root, completely different:

  • the other YAML consists at the root of a mapping that has values that are again mappings
  • this also has YAML has a mapping at the root, but its values are of different types after loading (integer, boolean, list)

Actually only the elements of the sequence that is the value for the root level somewhat correspond to the structure you have in the other question. But since these elements are mappings that include extra keys, the code needs to take that into account:

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())

which gives:

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'}
  • Related