简体   繁体   English

如何从python中的字典中提取丑陋的数据

[英]How to extract ugly data from dictionary in python

I am trying to write tests for my api, my response gets me ugly dictionary with only one value and lots of information in it, including list that i need to assert:我正在尝试为我的 api 编写测试,我的响应让我得到一个丑陋的字典,其中只有一个值和大量信息,包括我需要断言的列表:

{'data': OrderedDict([('count', 2), ('next', None), ('previous', None), ('results', [OrderedDict([('id', 1), ('uuid', '345cd6a7-69d2-4e35-808c-7bbfbee0ed66'), ('created_at', '2021-07-23T15:51:18.619102Z'), ('updated_at', '2021-07-23T15:51:18.619122Z'), ('deleted', False), ('name', 'Vegan'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)]), OrderedDict([('id', 2), ('uuid', 'e6a7ce08-259e-4a5b-bf96-2f311382b8ac'), ('created_at', '2021-07-23T15:51:18.619359Z'), ('updated_at', '2021-07-23T15:51:18.619373Z'), ('deleted', False), ('name', 'Dessert'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)])])]), 'code': 200, 'message': None}

I want to assert it with my serializer data which returns me a list which is the same as one list in my response dictionary value:我想用我的序列化程序数据断言它,它返回一个列表,该列表与我的响应字典值中的一个列表相同:

[OrderedDict([('id', 1), ('uuid', '345cd6a7-69d2-4e35-808c-7bbfbee0ed66'), ('created_at', '2021-07-23T15:51:18.619102Z'), ('updated_at', '2021-07-23T15:51:18.619122Z'), ('deleted', False), ('name', 'Vegan'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)]), OrderedDict([('id', 2), ('uuid', 'e6a7ce08-259e-4a5b-bf96-2f311382b8ac'), ('created_at', '2021-07-23T15:51:18.619359Z'), ('updated_at', '2021-07-23T15:51:18.619373Z'), ('deleted', False), ('name', 'Dessert'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)])]

if I could somehow extract that list from this dictionary it would be great, any help appreciated.如果我能以某种方式从这本词典中提取该列表,那就太好了,任何帮助表示赞赏。

Right from the repl:直接从repl:

>>> from collections import OrderedDict
>>> api = {'data': OrderedDict([('count', 2), ('next', None), ('previous', None), ('results', [OrderedDict([('id', 1), ('uuid', '345cd6a7-69d2-4e35-808c-7bbfbee0ed66'), ('created_at', '2021-07-23T15:51:18.619102Z'), ('updated_at', '2021-07-23T15:51:18.619122Z'), ('deleted', False), ('name', 'Vegan'),('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)]), OrderedDict([('id', 2), ('uuid', 'e6a7ce08-259e-4a5b-bf96-2f311382b8ac'), ('created_at', '2021-07-23T15:51:18.619359Z'), ('updated_at', '2021-07-23T15:51:18.619373Z'), ('deleted', False), ('name', 'Dessert'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)])])]), 'code': 200, 'message': None}
>>> api
{'data': OrderedDict([('count', 2), ('next', None), ('previous', None), ('results', [OrderedDict([('id', 1), ('uuid', '345cd6a7-69d2-4e35-808c-7bbfbee0ed66'), ('created_at', '2021-07-23T15:51:18.619102Z'), ('updated_at', '2021-07-23T15:51:18.619122Z'), ('deleted', False), ('name', 'Vegan'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)]), OrderedDict([('id', 2), ('uuid', 'e6a7ce08-259e-4a5b-bf96-2f311382b8ac'), ('created_at', '2021-07-23T15:51:18.619359Z'), ('updated_at', '2021-07-23T15:51:18.619373Z'), ('deleted', False), ('name', 'Dessert'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)])])]), 'code': 200, 'message':None}
>>> api['data']['results']
[OrderedDict([('id', 1), ('uuid', '345cd6a7-69d2-4e35-808c-7bbfbee0ed66'), ('created_at', '2021-07-23T15:51:18.619102Z'), ('updated_at', '2021-07-23T15:51:18.619122Z'), ('deleted', False), ('name', 'Vegan'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)]), OrderedDict([('id', 2), ('uuid', 'e6a7ce08-259e-4a5b-bf96-2f311382b8ac'), ('created_at', '2021-07-23T15:51:18.619359Z'), ('updated_at', '2021-07-23T15:51:18.619373Z'), ('deleted', False), ('name', 'Dessert'), ('address', ''), ('city', None), ('postal_code', None), ('country', None), ('phone', None), ('email', None)])]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM