在Python3中,如何将嵌套字典附加到搁置文件中,而使用for循环会增加难度?

在Python3中,如何将嵌套字典附加到搁置文件中,而使用for循环会增加难度?,python,python-3.x,dictionary,shelve,Python,Python 3.x,Dictionary,Shelve,我试图找出如何用嵌套字典填充搁置文件 以下是我的要求: 创建名为people.db的搁置文件 在搁置文件中添加新人员(person_1、person_2等) 使用for循环遍历“attributes”,这样我就不需要逐行写出冗长的代码来填充搁置文件 需要参考搁置文件数据以备将来使用 下面是我想附加到搁置文件的键/值格式 person_1 = { 'name': 'Bob', 'type': 'employee', 'attributes': [{'game': 'basketba

我试图找出如何用嵌套字典填充搁置文件

以下是我的要求:

  • 创建名为people.db的搁置文件
  • 在搁置文件中添加新人员(person_1、person_2等)
  • 使用for循环遍历“attributes”,这样我就不需要逐行写出冗长的代码来填充搁置文件
  • 需要参考搁置文件数据以备将来使用
  • 下面是我想附加到搁置文件的键/值格式

    person_1 = { 'name': 'Bob', 'type': 'employee', 'attributes':
            [{'game': 'basketball', 'high score': '100', 'time': '3.34'},
            {'game': 'bridge', 'high score': '10', 'time': '30.34'},
            {'game': 'foosball', 'high score': '2', 'time': '24'}]
            '''
            50+ other attributes
            '''
            }
    
    # Example: s['person_1]['attributes'][2]['time'] would call out '24'.
    # 's' is from 's = shelve.open('people')'
    
    我有一个dictionary dictPeople.py文件(使用包含person_1等信息的pprint()创建),以及 我只提取了所需数据的一小部分

    我尝试了以下操作,但出现无效密钥错误

    import shelve, dictPeople
    s = shelve.open('people')
    person = 'person_1'
    s[person]['name'] = dictPeople.person_1['name']
    s[person]['type'] = dictPeople.person_1['type']
    # I need to use this for loop because there are 50+ attributes.
    for attribute in range(0, len(dictPeople['attributes'][attribute])):
    s[person]['attributes'][attribute]['game'] = \
        dictPeople['attributes'][attribute]['game']
    s[person]['attributes'][attribute]['high score'] = \
        dictPeople['attributes'][attribute]['high score']
    s[person]['attributes'][attribute]['time'] = \
        dictPeople['attributes'][attribute]['time']
    
    结果是,我得到了键错误,因为我不允许引用键/值 就像我可以用字典配对一样。然而,疯狂的是我可以 尝试写入时,使用相同的精确格式调用db文件中的值

    例如: 我可以使用以下命令从.db文件读取数据:

    x = s['person_1']['name']
    print(x)
    
    但是!我不能用那种格式写入.db文件,否则我会得到一个无效的密钥 错误,毫无意义

    s['person_1']['name'] = 'Bob'
    # Returns invalid key entry.  Makes no sense.
    
    因此,我尝试使用以下方法提取数据并填充db文件:

    s[person] = {   'name': dictPeople.person_1['name'],
                'type': dictPeople.person_1['type'],
    for attribute in range(0, len(dictPeople['attributes'][attribute])):
        ['game':  dictPeople['attributes'][attribute]['game'],
        'high score': dictPeople['attributes'][attribute]['high score'],
        'time': dictPeople['attributes'][attribute]['time']]
    
    }
    
    但是,由于for循环,这显然不起作用。我该怎么做

  • 我试图找出如何从dictionary dictPeople.py文件中提取数据 并将其存储在people.db文件中
  • 随着更多人可用,我正在尝试向people.db文件添加新的人
  • 我需要使用for循环,因为我需要添加50多个属性
  • 我未来的步骤是修改我提取并用于填充people.db文件的一些数据值,但我的第一步至少是提取,然后 填充people.db文件

  • 当你打开它时,
    s
    是什么?它是一个空白的
    shelve.Shelf
    ?如果是这样,
    s[person][attribute]
    将给出一个
    keyrerror
    ,因为
    s[person]
    什么都不是。你可能必须用
    writeback=True
    打开它,然后执行
    s.setdefault(person,{})[attribute]=value
    或者如果您想避免
    写回=True
    (这会对性能造成严重影响),您可以执行
    tmp=s.get(person,{});tmp[attribute]=value;s[person]=tmp
    @AdamSmith感谢您的回复!您确实提供了帮助,我现在可以完成我的任务了。谢谢!