从具有重复键值对的重嵌套字典列表中提取值

2024-09-29 23:28:41 发布

您现在位置:Python中文网/ 问答频道 /正文

试图从复杂而混乱的字典列表中提取总现金和现金等价物值。该结构的缩短版本如下。你知道吗

我试过了:地图,Dataframe.from\ u目录&;来自\u记录。尽量避免使用RE。你知道吗

我被难住了。你知道吗

[{u'Fields': [], u'ReportDate': u'2 June 2016', u'ReportID': u'BalanceSheet', u'ReportName': u'Balance Sheet', u'ReportTitles': [u'Balance Sheet', u'Test Company', u'As at 30 June 2016'], u'ReportType': u'BalanceSheet', u'Rows': [{u'Cells': [{u'Value': u''}, {u'Value': u'30 Jun 2016'}, {u'Value': u'30 Jun 2015'}], u'RowType': u'Header'}, {u'RowType': u'Section', u'Rows': [], u'Title': u'Assets'}, {u'RowType': u'Section', u'Rows': [{u'Cells': [{u'Attributes': [{u'Id': u'account', u'Value': u'c0bxx922-cc31-4d53-b060-cbf23511`2533'}], u'Value': u'Test Bank 1'}, {u'Attributes': [{u'Id': u'account', u'Value': u'c1b4xx22-cc31-4d53-b060-cb45282533'}], u'Value': u'5555.20'}, {u'Attributes': [{u'Id': u'account', u'Value': u'c2b44922-cc31-4d53-b060-cbf4532582533'}], u'Value': u'5555.20'}], u'RowType': u'Row'}, {u'Cells': [{u'Attributes': [{u'Id': u'account', u'Value': u'290c7c3c-a712-4ads6f-9a2f-3d5258aad5a9e'}], u'Value': u'Test Bank 2'}, {u'Attributes': [{u'Id': u'account', u'Value': u'490c7c32-axxxdf6f-9a2f-3db682a3ad5a9e'}], u'Value': u'55555.20'}, {u'Attributes': [{u'Id': u'account', u'Value': u'490xxc3c-a71-adsf6f-9a2f-3d3aad5a9e'}], u'Value': u'55555.20'}], u'RowType': u'Row'}, {u'Cells': [{u'Attributes': [{u'Id': u'account', u'Value': u'c6d4da40-f0df1b0-8f7d-xx45b1405'}], u'Value': u'Test Bank 3'}, {u'Attributes': [{u'Id': u'account', u'Value': u'c6d4da4fg-df-41b0-8f7d-54xx345b1405'}], u'Value': u'5555.20'}, {u'Attributes': [{u'Id': u'account', u'Value': u'c6d4dafgss-9-41b0-8f7d-60xx5b1405'}], u'Value': u'5555.20'}], u'RowType': u'Row'}, {u'Cells': [{u'Value': u'Total Cash and Cash Equivalents'}, {u'Value': u'5555555.20'}, {u'Value': u'5555555.20'}], u'RowType': u'SummaryRow'}], u'Title': u'Cash and Cash Equivalents'}, {u'RowType': u'Section',

Tags: testidvaluesectionaccountcashattributesrows
1条回答
网友
1楼 · 发布于 2024-09-29 23:28:41

如果您知道数据的格式与上面的格式完全相同,并且您真的只需要这两个值,那么您可以直接访问它(假设data是您上面的结构):

print data[0]['Rows'][2]['Rows'][3]['Cells'][1]['Value']
print data[0]['Rows'][2]['Rows'][3]['Cells'][2]['Value']

但是,无论是在写下正确的表达式时,还是在更改列表的顺序时(在您的格式中可能没有定义),这都是容易出错的。由于数据背后有一个语义结构,您可以将原始数据转换回一个易于访问的对象。您可能想更改一些细节,但这是一个很好的起点:

from collections import Mapping
import pandas as pd

class Report(Mapping):
    def __init__(self, data):
        self.sections = OrderedDict()
        for row in data.pop('Rows'):
            getattr(self, 'make_%s' % row['RowType'])(row)
        self.__dict__.update(data)

    def make_Header(self, row):
        self.header = [c['Value'] for c in row['Cells']]

    def make_Section(self, sec):
        def make_row(row):
            cells = [c['Value'] for c in row['Cells']]
            return pd.Series(map(float, cells[1:]), name=cells[0])

        self.sections[sec['Title']] = pd.DataFrame(make_row(r) for r in sec['Rows'])

    def __getitem__(self, item):
        return self.sections[item]

    def __len__(self):
        return len(self.sections)

    def __iter__(self):
        return iter(self.sections)


report = Report(data[0])
print report.ReportName
print report['Cash and Cash Equivalents']

相关问题 更多 >

    热门问题