首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用随机数替换YAML文件中的数字的问题

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

Stack Overflow用户
提问于 2022-04-23 11:54:04
回答 1查看 76关注 0票数 1

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

代码语言:javascript
运行
复制
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())

我正在编辑的文件是:

代码语言:javascript
运行
复制
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文件的第一行相同的文件结构。我在做什么假的?有关代码的更多信息,请访问this question

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-23 13:38:04

您的输入类似于另一个问题,但从结构上讲,从根本上讲,完全不同:

  • (另一个YAML )包含一个映射的根,该映射的值也是
  • ,它的根也有一个映射,但在加载(整数、布尔、列表)

之后,它的值是不同的类型。

实际上,只有序列的元素,即根级别的值,在某种程度上与您在另一个问题中的结构相对应。但是,由于这些元素是包含额外键的映射,所以代码需要考虑到这一点:

代码语言:javascript
运行
复制
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())

这意味着:

代码语言:javascript
运行
复制
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'}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71979377

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档