我有一個深度嵌套的資料以字典的形式呈現,其中包含串列和字典的混合(例如字典 -> 字典串列 -> 字典 -> 字典串列)。
我想用glom決議它的值(希望是pandas-dataframeable),但是在glom的規范中混合字典和串列時遇到了困難。
我可以通過鏈接字典鍵和索引串列來訪問每個單獨的值,如下所示:
dict['TradePlaceList'][0]['TradeList'][0]['Trade']['Message']['soap:Envelope']['soap:Body']['SetBiddingProcessInfo']
但需要幫助在glom的規范中表達相同的邏輯。
資料示例(因大小而減少):
{'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')]))]))]))]))]))]))]))]),
uj5u.com熱心網友回復:
以下 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', ['TradeList', ['Trade.Message.soap:Envelope.soap:Body.SetBiddingProcessInfo']])))
[]這是使用您發布的路徑,以及在 glom中使用的迭代串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410054.html
標籤:
下一篇:Python類中的字典邏輯
