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.