动态地将python字典存储在JSON文件中

时间:2016-11-02 10:09:43

标签: python json dictionary

我有一个python字典,其中的键格式为Aa123,每个值都是一个列表。现在我需要以某种格式或任何数据库永久存储这些数据,以便我可以轻松地检索它。我认为JSON会更好,并尝试以JSON格式存储所有数据,然后一些应用程序可以使用此文件。我的JSON格式应该是,

[
  {  "firstLetter" : "A",
     "remaining" : [
        {
           "secondLetter" : "a",
           "ID" : [
              {"id" : "Aa123", "listOfItems" : ["ABC123","ASD100"]},
              {"id" : "Aa100", "listOfItems" : ["ABC123","COD101"]}
           ]
        },
        {
           "secondLetter" : "b",
           "ID" : [
              {"id" : "Ab100", "listOfItems" : ["ABC123","ASD100"]}
           ]
        }
     ]
  },
  {  "firstLetter" : "B",
     "remaining" : [
        {
           "secondLetter" : "a",
           "ID" : [                  
              {"id" : "Ba106", "listOfItems" : ["AUD123","CML101"]}
           ]
        },
        {
           "secondLetter" : "b",
           "ID" : [
              {"id" : "Bb153", "listOfItems" : ["AER113","ASD100"]},
              {"id" : "Bb100", "listOfItems" : ["ATC123","ASD500"]}
           ]
        }
     ]
  }
]

我知道如何将python字典转储为JSON,但这并不能提供简单的数据查询。我的问题是“如何以所需格式存储这个python字典(我通过运行python程序获得)(如上所示),使数据查询变得容易。谢谢!

5 个答案:

答案 0 :(得分:2)

听起来你可能会受益于tinydb。它将值直接存储在JSON文件中,并提供查询方法。

所以为了存储你的价值,我们做

from tinydb import TinyDB, Query

db = TinyDB('test.json')
values = [{'firstLetter': 'A',
  'remaining': [{'ID': [{'id': 'Aa123', 'listOfItems': ['ABC123', 'ASD100']},
     {'id': 'Aa100', 'listOfItems': ['ABC123', 'COD101']}],
    'secondLetter': 'a'},
   {'ID': [{'id': 'Ab100', 'listOfItems': ['ABC123', 'ASD100']}],
    'secondLetter': 'b'}]},
 {'firstLetter': 'B',
  'remaining': [{'ID': [{'id': 'Ba106', 'listOfItems': ['AUD123', 'CML101']}],
    'secondLetter': 'a'},
   {'ID': [{'id': 'Bb153', 'listOfItems': ['AER113', 'ASD100']},
     {'id': 'Bb100', 'listOfItems': ['ATC123', 'ASD500']}],
    'secondLetter': 'b'}]}]
for value in values:
    db.insert(value)

要查询,我们

>>> Q = Query()
>>> db.search(Q.firstLetter == "A")

[{'firstLetter': 'A',
  'remaining': [{'ID': [{'id': 'Aa123', 'listOfItems': ['ABC123', 'ASD100']},
     {'id': 'Aa100', 'listOfItems': ['ABC123', 'COD101']}],
    'secondLetter': 'a'},
   {'ID': [{'id': 'Ab100', 'listOfItems': ['ABC123', 'ASD100']}],
    'secondLetter': 'b'}]}]

答案 1 :(得分:1)

除了MongoDBCassandra等NoSQL数据库之外,如果您使用的是PostgreSQL,请查看hstore数据类型,特别是psycopg2' s {{3}功能。如果您希望能够更轻松地查询数据,这可能会有所帮助。

答案 2 :(得分:1)

在下面的示例中,我将数据输出到JSON,并使用Shelves存储dict

import json
import codecs
import shelve

formatted_item = {
    "data": [
      {  "firstLetter" : "A",
         "remaining" : [
            {
               "secondLetter" : "a",
               "ID" : [
                  {"id" : "Aa123", "listOfItems" : ["ABC123","ASD100"]},
                  {"id" : "Aa100", "listOfItems" : ["ABC123","COD101"]}
               ]
            },
            {
               "secondLetter" : "b",
               "ID" : [
                  {"id" : "Ab100", "listOfItems" : ["ABC123","ASD100"]}
               ]
            }
         ]
      },
      {  "firstLetter" : "B",
         "remaining" : [
            {
               "secondLetter" : "a",
               "ID" : [                  
                  {"id" : "Ba106", "listOfItems" : ["AUD123","CML101"]}
               ]
            },
            {
               "secondLetter" : "b",
               "ID" : [
                  {"id" : "Bb153", "listOfItems" : ["AER113","ASD100"]},
                  {"id" : "Bb100", "listOfItems" : ["ATC123","ASD500"]}
               ]
            }
         ]
      }
    ]
}


###
# JSON

# *** Store

output = json.dumps(dict(formatted_item), sort_keys=True, indent=4, separators=(',', ': '))
json_file_path = 'temp.json'
with codecs.open(json_file_path, 'wb', encoding='utf8') as file:
    file.write(output)

# *** Read

with open(json_file_path) as json_file:
    json_data = json.load(json_file)

# *** Print

print json_data
print json_data['data'][0]['firstLetter']


###
# Shelves

# *** Store

shelf_file_path = 'temp.log'
shelf = shelve.open(shelf_file_path)
shelf.update(formatted_item)
shelf.close()

# *** Read

loaded_shelf = shelve.open(shelf_file_path)

# *** Print

print loaded_shelf
print loaded_shelf['data'][0]['firstLetter']

答案 3 :(得分:0)

您可以直接在MongoDB上保存Python Dict对象

有关详细信息,请参阅: http://api.mongodb.com/python/current/tutorial.html

答案 4 :(得分:-1)

DBMS不能很好地处理开箱即用的JSON查询。

您现在可以将JSON文件存储在支持的RDBMS,NoSQL中以便快速查询。尽管如此,支持JSON数据类型的DBMS,通过略读json结构并大量索引它们来尝试解决方法。在处理json数据类型时,NoSQL并不比RDBMS好。

因此,在存储json文件之前,必须始终阅读特定的RDBMS / NoSQL文档,尤其是当您有大量的json数据时。