Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python从json数据库提取特定字符串的最佳方法是什么?_Python_Database_Python 3.x - Fatal编程技术网

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

使用python从json数据库提取特定字符串的最佳方法是什么?,python,database,python-3.x,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

我有这样一个大型数据库:

{'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都被提取出来


感谢具有深度嵌套结构的可以帮助您:

>>> 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}]}
所以。您的dict只有一个键:
gives

>>> 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}]
您有一个dict列表,每个dict都有一个
id
键。使用列表理解,您就完成了:

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

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

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)
这将返回:

[603988529, 603988528, 603988527, 603988526, 603988525, 603988524, 603988523, 603988522]

the_id=[item[“id”]for item in your_obj['gifts']]
where
your_obj
是您发布的任何对象?嗯,用一个字符串解析json,然后从dicts?@user3159253中提取值。这不是有效的json,我假设它已经解析为python对象。这是一个字典,不是json或数据库。您尝试过解析它什么?嗨,eric,我尝试过pprint(数据),但它在pprint(数据)TypeError中的第1行显示了回溯(最近一次调用):文件“”,“模块”对象不可用callable@cromat:我忘记了pprint导入pprint的
。我更新了答案。