使用python从json数据库中提取某些字符串的最佳方法是什么?

时间:2017-11-21 07:41:56

标签: python database python-3.x

我有一个大型数据库,例如:

{'gifts': [{'id': 603988529, 'created_at': 1511248537, 'item_id': 11, 'item_type': 'Point::Stone', 'quantity': 1, 'description': 'Compensation for the incorrect item names and an incorrect banner description'}, {'id': 603988528, 'created_at': 1511248537, 'item_id': 932, 'item_type': 'SpecialItem', 'quantity': 2, 'description': '"Dokkan Thank-You Celebration" Login Bonus!'}, {'id': 603988527, 'created_at': 1511248537, 'item_id': 931, 'item_type': 'SpecialItem', 'quantity': 2, 'description': '"Dokkan Thank-You Celebration" Login Bonus!'}, {'id': 603988526, 'created_at': 1511248537, 'item_id': 1004, 'item_type': 'TrainingItem', 'quantity': 5, 'description': '"Dokkan Thank-You Celebration" Login Bonus!'}, {'id': 603988525, 'created_at': 1511248537, 'item_id': 1003, 'item_type': 'TrainingItem', 'quantity': 5, 'description': '"Dokkan Thank-You Celebration" Login Bonus!'}, {'id': 603988524, 'created_at': 1511248537, 'item_id': 1002, 'item_type': 'TrainingItem', 'quantity': 5, 'description': '"Dokkan Thank-You Celebration" Login Bonus!'}, {'id': 603988523, 'created_at': 1511248537, 'item_id': 1001, 'item_type': 'TrainingItem', 'quantity': 5, 'description': '"Dokkan Thank-You Celebration" Login Bonus!'}, {'id': 603988522, 'created_at': 1511248537, 'item_id': 1000, 'item_type': 'TrainingItem', 'quantity': 5, 'description': '"Dokkan Thank-You Celebration" Login Bonus!'}]}

我想写一些代码来提取" ID"之后的每个数字。并把它放在一张桌子里。

这样我才能获得[603988529,603988528 ....],直到提取出所有ID

由于

2 个答案:

答案 0 :(得分:0)

使用深层嵌套的结构,pprint可以帮助您:

>>> from pprint import pprint
>>> pprint(data)
{'gifts': [{'created_at': 1511248537,
            'description': 'Compensation for the incorrect item names and an '
                           'incorrect banner description',
            'id': 603988529,
            'item_id': 11,
            'item_type': 'Point::Stone',
            'quantity': 1},
           {'created_at': 1511248537,
            'description': '"Dokkan Thank-You Celebration" Login Bonus!',
            'id': 603988528,
            'item_id': 932,
            'item_type': 'SpecialItem',
            'quantity': 2},
           {'created_at': 1511248537,
            'description': '"Dokkan Thank-You Celebration" Login Bonus!',
            'id': 603988527,
            'item_id': 931,
            'item_type': 'SpecialItem',
            'quantity': 2},
           {'created_at': 1511248537,
            'description': '"Dokkan Thank-You Celebration" Login Bonus!',
            'id': 603988526,
            'item_id': 1004,
            'item_type': 'TrainingItem',
            'quantity': 5},
           {'created_at': 1511248537,
            'description': '"Dokkan Thank-You Celebration" Login Bonus!',
            'id': 603988525,
            'item_id': 1003,
            'item_type': 'TrainingItem',
            'quantity': 5},
           {'created_at': 1511248537,
            'description': '"Dokkan Thank-You Celebration" Login Bonus!',
            'id': 603988524,
            'item_id': 1002,
            'item_type': 'TrainingItem',
            'quantity': 5},
           {'created_at': 1511248537,
            'description': '"Dokkan Thank-You Celebration" Login Bonus!',
            'id': 603988523,
            'item_id': 1001,
            'item_type': 'TrainingItem',
            'quantity': 5},
           {'created_at': 1511248537,
            'description': '"Dokkan Thank-You Celebration" Login Bonus!',
            'id': 603988522,
            'item_id': 1000,
            'item_type': 'TrainingItem',
            'quantity': 5}]}

因此。你有一个只有一个密钥的字典:gifts

>>> pprint(data['gifts'])
[{'created_at': 1511248537,
  'description': 'Compensation for the incorrect item names and an incorrect '
                 'banner description',
  'id': 603988529,
  'item_id': 11,
  'item_type': 'Point::Stone',
  'quantity': 1},
 {'created_at': 1511248537,
  'description': '"Dokkan Thank-You Celebration" Login Bonus!',
  'id': 603988528,
  'item_id': 932,
  'item_type': 'SpecialItem',
  'quantity': 2},
 {'created_at': 1511248537,
  'description': '"Dokkan Thank-You Celebration" Login Bonus!',
  'id': 603988527,
  'item_id': 931,
  'item_type': 'SpecialItem',
  'quantity': 2},
 {'created_at': 1511248537,
  'description': '"Dokkan Thank-You Celebration" Login Bonus!',
  'id': 603988526,
  'item_id': 1004,
  'item_type': 'TrainingItem',
  'quantity': 5},
 {'created_at': 1511248537,
  'description': '"Dokkan Thank-You Celebration" Login Bonus!',
  'id': 603988525,
  'item_id': 1003,
  'item_type': 'TrainingItem',
  'quantity': 5},
 {'created_at': 1511248537,
  'description': '"Dokkan Thank-You Celebration" Login Bonus!',
  'id': 603988524,
  'item_id': 1002,
  'item_type': 'TrainingItem',
  'quantity': 5},
 {'created_at': 1511248537,
  'description': '"Dokkan Thank-You Celebration" Login Bonus!',
  'id': 603988523,
  'item_id': 1001,
  'item_type': 'TrainingItem',
  'quantity': 5},
 {'created_at': 1511248537,
  'description': '"Dokkan Thank-You Celebration" Login Bonus!',
  'id': 603988522,
  'item_id': 1000,
  'item_type': 'TrainingItem',
  'quantity': 5}]

您有一个dicts列表,每个dict都有一个id键。使用列表理解并完成:

>>> [d['id'] for d in data['gifts']]
[603988529, 603988528, 603988527, 603988526, 603988525, 603988524, 603988523, 603988522]

答案 1 :(得分:0)

将字符串保存到文件中以放入变量中,就像您询问API时得到的答复一样:

this.router.events.filter((routerEvent) => routerEvent instanceof NavigationEnd)
  .subscribe(() => this._mont.showMap(+this._route.snapshot.params['id']));
}

这将返回:

text_file = open("json.txt", "r")
array = text_file.read().replace('"', '\\"').replace("'", '"')
data  = json.loads(array)
idlist = []
for ids in data['gifts']:
    idlist.append(ids['id'])

print(idlist)