有没有一种方法可以将两个值附加到带有for循环和嵌套for循环的python中的json对象中?

时间:2019-08-02 21:25:01

标签: arrays for-loop python-3.7.4

我正在制作游戏,并且试图通过json格式保存数据,以便于访问和操纵。

这是我正在制作的游戏,我将通过JSON格式保存和加载数据,并尝试在写入JSON对象之前将两个不同的数组索引写入到JSON对象中。

这是python代码

# skills
self.skills = ['accuracy', 'strength', 'sneaking', 'looting', 'scouting', 'cooking', 'fishing', 'agility']
self.skill_levels = [0, 0, 0, 0, 0, 0, 0, 0]

def write_to_skills_json(self):
    # creating the json data object
    data = {'skills': []}
    # adds the skills into the data json object
    for x in range(0, len(self.skills)):
       for y in range(0, len(self.skill_levels)):
           data['skills'].append({'name': self.skills[x], 'level': self.skill_levels[y]})
    with open('data.json', 'w') as outfile:
        json.dump(data, outfile)
    return

这是json代码(这有点长,因为它破坏了整个文件,请提前对不起)

{"skills": [{"name": "accuracy", "level": 0}, {"name": "accuracy", "level": 0}, {"name": "accuracy", "level": 0}, {"name": "accuracy", "level": 0}, {"name": "accuracy", "level": 0}, {"name": "accuracy", "level": 0}, {"name": "accuracy", "level": 0}, {"name": "accuracy", "level": 0}, {"name": "strength", "level": 0}, {"name": "strength", "level": 0}, {"name": "strength", "level": 0}, {"name": "strength", "level": 0}, {"name": "strength", "level": 0}, {"name": "strength", "level": 0}, {"name": "strength", "level": 0}, {"name": "strength", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "sneaking", "level": 0}, {"name": "looting", "level": 0}, {"name": "looting", "level": 0}, {"name": "looting", "level": 0}, {"name": "looting", "level": 0}, {"name": "looting", "level": 0}, {"name": "looting", "level": 0}, {"name": "looting", "level": 0}, {"name": "looting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "scouting", "level": 0}, {"name": "cooking", "level": 0}, {"name": "cooking", "level": 0}, {"name": "cooking", "level": 0}, {"name": "cooking", "level": 0}, {"name": "cooking", "level": 0}, {"name": "cooking", "level": 0}, {"name": "cooking", "level": 0}, {"name": "cooking", "level": 0}, {"name": "fishing", "level": 0}, {"name": "fishing", "level": 0}, {"name": "fishing", "level": 0}, {"name": "fishing", "level": 0}, {"name": "fishing", "level": 0}, {"name": "fishing", "level": 0}, {"name": "fishing", "level": 0}, {"name": "fishing", "level": 0}, {"name": "agility", "level": 0}, {"name": "agility", "level": 0}, {"name": "agility", "level": 0}, {"name": "agility", "level": 0}, {"name": "agility", "level": 0}, {"name": "agility", "level": 0}, {"name": "agility", "level": 0}, {"name": "agility", "level": 0}]}

基本上,我希望输出在data.json文件中,该文件一次保存技能名称和技能级别。然后可以重新打开文件并将其重写几次。然而;一直没有得到我想要的结果,我已经重复了八次相同的技能名称和等级。

(对不起,如果我没有真正解释清楚我想做的事情,我对python还是很陌生,不知道如何解释我想做的很多事情,我只是知道该怎么做。)

0 个答案:

没有答案