【问题标题】:Parsing deeply nested dictionary with python & glom用 python 和 glom 解析深度嵌套的字典
【发布时间】:2022-01-08 21:12:15
【问题描述】:

我有一个深度嵌套的数据以字典的形式呈现,其中包含列表和字典(例如字典 -> 字典列表 -> 字典 -> 字典列表)。

我想用 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')]))]))]))]))]))]))]))]),                         

【问题讨论】:

    标签: python json pandas dictionary glom


    【解决方案1】:

    以下 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 中使用 [] 迭代列表。

    【讨论】:

    • 是否可以创建一个规范来获取列出的列表中的所有必需值?类似于从 0 到 len(list) 的嵌套列表循环?
    • @Krank 是的,我刚刚添加了一个如何做到这一点的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-05
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2019-01-18
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多