随机播放 YAML 文件中的内容
Shuffle content in a YAML file
我有这个 YAML 文件,我有一个列表,其中包含我想要随机播放的字符串:
名单:['StmRsBgmSea02', 'StmRsBgmShopRadio', 'StmRsBgmSea01', ...]
这不是完整列表。
YAML 文件:
Version: 3
IsBigEndian: False
SupportPaths: False
HasReferenceNodes: False
root:
StageInfoList:
- Name: SeaWorldHomeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: WorldMain, ResourceName: StmRsBgmSea02}
- {Name: Shop, ResourceName: StmRsBgmShopRadio}
- ScenarioNo: !l 1
StagePlayInfoList:
- {Name: WorldMain, ResourceName: StmRsBgmSea01}
- {Name: Boss, ResourceName: StmRsBgmBossHaikai_B}
- {Name: Town, ResourceName: StmRsBgmSea01beach}
- {Name: BossDead, ResourceName: WsdRsBgmJgSeaFountain}
- Name: SeaWorldCostumeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: CostumeRoom, ResourceName: WsdRsBgmMarioPlayHula}
文件长 800 行。我有一个列表,其中包含后面的每个名字
ResourceName:
我希望 ResourceName: 之后的每个值都将替换为列表中的一个随机值。重要的是,在一个 RessourceName 被随机化后,该字符串将从列表中删除。这样就没有值出现两次了。
当您从 YAML(或 JSON)加载数据时,您可以轻松地在数据结构中递归:一切
是字典、列表或值。你应该制定一个例行程序
替换值列表(此处为 [v0, v1, v2, ...]
,同时递归并分配它们
每当您找到名为 ResourceName
.
的密钥时
import sys
from pathlib import Path
import random
import ruamel.yaml
def recur(d, sub, key):
if isinstance(d, dict):
for k, v in d.items():
if k == key:
d[key] = selected = random.choice(sub)
sub.remove(selected)
continue
recur(v, sub, key)
elif isinstance(d, list):
for elem in d:
recur(elem, sub, key)
substitutes =['v0', 'v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9']
yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
data = yaml.load(Path('input.yaml'))
recur(data, substitutes, 'ResourceName')
yaml.dump(data, sys.stdout)
给出:
Version: 3
IsBigEndian: false
SupportPaths: false
HasReferenceNodes: false
root:
StageInfoList:
- Name: SeaWorldHomeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: WorldMain, ResourceName: v1}
- {Name: Shop, ResourceName: v5}
- ScenarioNo: !l 1
StagePlayInfoList:
- {Name: WorldMain, ResourceName: v2}
- {Name: Boss, ResourceName: v0}
- {Name: Town, ResourceName: v4}
- {Name: BossDead, ResourceName: v3}
- Name: SeaWorldCostumeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: CostumeRoom, ResourceName: v7}
如果合适,您可能需要检查替代品是否为空
在调用 recur()
之后
我有这个 YAML 文件,我有一个列表,其中包含我想要随机播放的字符串:
名单:['StmRsBgmSea02', 'StmRsBgmShopRadio', 'StmRsBgmSea01', ...]
这不是完整列表。
YAML 文件:
Version: 3
IsBigEndian: False
SupportPaths: False
HasReferenceNodes: False
root:
StageInfoList:
- Name: SeaWorldHomeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: WorldMain, ResourceName: StmRsBgmSea02}
- {Name: Shop, ResourceName: StmRsBgmShopRadio}
- ScenarioNo: !l 1
StagePlayInfoList:
- {Name: WorldMain, ResourceName: StmRsBgmSea01}
- {Name: Boss, ResourceName: StmRsBgmBossHaikai_B}
- {Name: Town, ResourceName: StmRsBgmSea01beach}
- {Name: BossDead, ResourceName: WsdRsBgmJgSeaFountain}
- Name: SeaWorldCostumeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: CostumeRoom, ResourceName: WsdRsBgmMarioPlayHula}
文件长 800 行。我有一个列表,其中包含后面的每个名字
ResourceName:
我希望 ResourceName: 之后的每个值都将替换为列表中的一个随机值。重要的是,在一个 RessourceName 被随机化后,该字符串将从列表中删除。这样就没有值出现两次了。
当您从 YAML(或 JSON)加载数据时,您可以轻松地在数据结构中递归:一切
是字典、列表或值。你应该制定一个例行程序
替换值列表(此处为 [v0, v1, v2, ...]
,同时递归并分配它们
每当您找到名为 ResourceName
.
import sys
from pathlib import Path
import random
import ruamel.yaml
def recur(d, sub, key):
if isinstance(d, dict):
for k, v in d.items():
if k == key:
d[key] = selected = random.choice(sub)
sub.remove(selected)
continue
recur(v, sub, key)
elif isinstance(d, list):
for elem in d:
recur(elem, sub, key)
substitutes =['v0', 'v1', 'v2', 'v3', 'v4', 'v5', 'v6', 'v7', 'v8', 'v9']
yaml = ruamel.yaml.YAML()
yaml.indent(mapping=2, sequence=4, offset=2)
data = yaml.load(Path('input.yaml'))
recur(data, substitutes, 'ResourceName')
yaml.dump(data, sys.stdout)
给出:
Version: 3
IsBigEndian: false
SupportPaths: false
HasReferenceNodes: false
root:
StageInfoList:
- Name: SeaWorldHomeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: WorldMain, ResourceName: v1}
- {Name: Shop, ResourceName: v5}
- ScenarioNo: !l 1
StagePlayInfoList:
- {Name: WorldMain, ResourceName: v2}
- {Name: Boss, ResourceName: v0}
- {Name: Town, ResourceName: v4}
- {Name: BossDead, ResourceName: v3}
- Name: SeaWorldCostumeStage
StageScenarioInfoList:
- ScenarioNo: !l 0
StagePlayInfoList:
- {Name: CostumeRoom, ResourceName: v7}
如果合适,您可能需要检查替代品是否为空
在调用 recur()