1 回答

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
錯(cuò)誤來自quest['quests']
. 您的數(shù)據(jù)是一個(gè)字典,其中一個(gè)條目名為quests
:
for quest in data: print(quest) # will just print "quests"
要正確迭代您的 yaml,您需要:
獲取任務(wù)字典,使用
data["quests"]
對(duì)于 quests 字典中的每個(gè)條目,使用條目鍵作為文件名并轉(zhuǎn)儲(chǔ)文件中的條目值。
這是您的腳本的修補(bǔ)版本:
def splitfile():
try:
with open(input, "r") as stream:
data = yaml.load(stream)
quests = data['quests'] # get the quests dictionary
for name, quest in quests.items():
# .items() returns (key, value),
# here name and quest attributes
outfile = open("Quests/quests/" + name + ".yml", "x")
yaml.dump(quest, outfile)
except yaml.YAMLError as out:
print(out)
splitfile()
添加回答
舉報(bào)