[英]Parsing deeply nested dictionary with python & glom
I have a deeply nested data presented as dictionary with mix of lists & dicts (eg Dict -> List of dicts -> Dict -> List of dicts).我有一个深度嵌套的数据以字典的形式呈现,其中包含列表和字典的混合(例如字典 -> 字典列表 -> 字典 -> 字典列表)。
I'd like to parse it's values with glom (hopefully to something pandas-dataframeable), but struggling with mixing dicts & lists in glom's spec.我想用glom解析它的值(希望是pandas-dataframeable),但是在glom的规范中混合字典和列表时遇到了困难。
I can access each individual value by chaining dict keys & indexing lists like this:我可以通过链接字典键和索引列表来访问每个单独的值,如下所示:
dict['TradePlaceList'][0]['TradeList'][0]['Trade']['Message']['soap:Envelope']['soap:Body']['SetBiddingProcessInfo']
but need help in expressing the same logic in glom's spec.但需要帮助在glom的规范中表达相同的逻辑。
Data example (reduced due to size):数据示例(因大小而减少):
{'TradePlaceList': [OrderedDict([('@INN', '6164265896'),
('TradeList',
[OrderedDict([('Trade',
OrderedDict([('@ID_EFRSB', '1201661'),
('@ID_EXTERNAL', 'ПП-49739'),
('Message',
OrderedDict([('@ID', '10958517'),
('soap:Envelope',
OrderedDict([('@xmlns:xsi',
'http://www.w3.org/2001/XMLSchema-instance'),
('@xmlns:xsd',
'http://www.w3.org/2001/XMLSchema'),
('@xmlns:soap',
'http://schemas.xmlsoap.org/soap/envelope/'),
('soap:Body',
OrderedDict([('SetBiddingProcessInfo',
OrderedDict([('@xmlns',
'https://services.fedresurs.ru/BiddingService2'),
('BiddingProcessInfo',
OrderedDict([('@TradeId',
'ПП-49739'),
('@EventTime',
'2021-05-03T00:00:00'),
('PriceInfo',
OrderedDict([('@LotNumber',
'1'),
('@NewPrice',
'3049997.96')]))]))]))]))]))]))]))]),
The following glom spec works on the example you posted:以下 glom 规范适用于您发布的示例:
import pandas as pd
import glom
from collections import OrderedDict
data = {'TradePlaceList': [OrderedDict([('@INN', '6164265896'),
('TradeList',
[OrderedDict([('Trade',
OrderedDict([('@ID_EFRSB', '1201661'),
('@ID_EXTERNAL', 'ПП-49739'),
('Message',
OrderedDict([('@ID', '10958517'),
('soap:Envelope',
OrderedDict([('@xmlns:xsi',
'http://www.w3.org/2001/XMLSchema-instance'),
('@xmlns:xsd',
'http://www.w3.org/2001/XMLSchema'),
('@xmlns:soap',
'http://schemas.xmlsoap.org/soap/envelope/'),
('soap:Body',
OrderedDict([('SetBiddingProcessInfo',
OrderedDict([('@xmlns',
'https://services.fedresurs.ru/BiddingService2'),
('BiddingProcessInfo',
OrderedDict([('@TradeId',
'ПП-49739'),
('@EventTime',
'2021-05-03T00:00:00'),
('PriceInfo',
OrderedDict([('@LotNumber',
'1'),
('@NewPrice',
'3049997.96')]))]))]))]))]))]))]))])])])]}
print(glom.glom(data, 'TradePlaceList.0.TradeList.0.Trade.Message.soap:Envelope.soap:Body.SetBiddingProcessInfo'))
I pretty much just removed the brackets.我几乎只是删除了括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.