【问题标题】:How to work with nested list of dicts for docxtpl jinja2 tags in python如何在python中使用docxtpl jinja2标签的嵌套字典列表
【发布时间】:2021-04-24 06:38:00
【问题描述】:

我有一个包含列表字典的列表,其中包含包含键/值对字典的列表字典。它看起来像这样:

super_data: [{'data': [{'attributes': {'company_name': 'Test Ltd.',
                           'created_at': '2011-03-31T22:24:37.000Z',
                           'description': 'Department: Testing Department\r\n'
                                          'Salary: 100000\r\n'
                                          'Bonus: 25000\r\n'
                                          'Position Type: Full Time\r\n'
                                          'Reference: 0\r\n'
                                          'Company Confirmed: 0\r\n'
                                          'Candidate Confirmed: 0\r\n'
                                          'Signing: 0\r\n'
                                          'Performance: -1\r\n'
                                          'Car: -1\r\n'
                                          'Stock: -1\r\n'
                                          'Club: -1\r\n'
                                          'Performance Details: 50%\r\n',
                           'from': '2003-01-02',
                           'name': 'Chief Testing Officer',
                           'present': True,
                           'to': None,
                           'updated_at': '2020-07-21T15:16:05.961Z'},
            'id': '86735',
            'relationships': {},
            'type': 'positions'},
           {'attributes': {'company_name': 'Test Ltd.',
                           'created_at': '2011-03-31T22:24:37.000Z',
                           'description': 'Salary: 60000\r\n',
                           'from': '2002-01-02',
                           'name': 'Chief Operating Officer',
                           'present': False,
                           'to': '2003-01-02',
                           'updated_at': '2020-07-07T21:03:00.213Z'},
            'id': '8976543',
            'relationships': {},
            'type': 'positions'}],
  'links': {'first': 'https://api.test.com/api/v5/contacts/65789/positions?offset=0',
            'next': None,
            'prev': None},
  'meta': {'count': 2, 'offset': 0}},
 {'data': [{'attributes': {'company_name': 'Tester',
                           'created_at': '2020-01-13T16:15:58.313Z',
                           'description': None,
                           'from': '2013-03-01',
                           'name': 'Chief Operating and Financial Officer',
                           'present': True,
                           'to': None,
                           'updated_at': '2020-05-21T21:45:53.183Z'},
            'id': '354833',
            'relationships': {},
            'type': 'positions'},
           {'attributes': {'company_name': 'Testy',
                           'created_at': '2020-01-13T16:15:58.470Z',
                           'description': 
                           'from': '2008-02-01',
                           'name': 'Chief Financial Officer',
                           'present': False,
                           'to': '2013-03-01',
                           'updated_at': '2020-01-13T16:15:58.470Z'},
            'id': '53435',
            'relationships': {},
            'type': 'positions'},
           {'attributes': {'company_name': 'Test 3',
                           'created_at': '2020-01-13T16:15:58.627Z',
                           'description': 
                           'from': '2000-01-01',
                           'name': 'Chief Financial Officer'
                                   'Partners',
                           'present': False,
                           'to': '2007-01-01',
                           'updated_at': '2020-01-13T16:15:58.627Z'},
            'id': '876534',
            'relationships': {},
            'type': 'positions'},
           {'attributes': {'company_name': 'Test4',
                           'created_at': '2020-01-13T16:15:58.780Z',
                           'description':
                           'from': '1996-01-01',
                           'name': 'Chief Financial Officer',
                           'present': False,
                           'to': '2000-01-01',
                           'updated_at': '2020-01-13T16:15:58.780Z'},
            'id': '65435',
            'relationships': {},
            'type': 'positions'},
           {'attributes': {'company_name': 'Test5',
                           'created_at': '2020-01-13T16:15:59.291Z',
                           'description': None,
                           'from': '1992-01-01',
                           'name': 'Auditor',
                           'present': False,
                           'to': '1996-01-01',
                           'updated_at': '2020-01-13T16:15:59.291Z'},
            'id': '4654654',
            'relationships': {},
            'type': 'positions'}],
  'links': {'first': 'https://api.test.com/api/v5/contacts/45687/positions?offset=0',
            'next': None,
            'prev': None},
  'meta': {'count': 5, 'offset': 0}}]

我正在尝试使用它在使用 docxtpl 和 jinja2 标签的 word 模板中的单元格中放置特定值。

例如:我希望能够让列中的第一个单元格包含第一个“数据”集的所有“属性”集中的所有“公司名称”键和“名称”键的值,然后在列中的后续单元格中包含下一个。

所以我试图让它看起来像这样:

                 Jobs
______________________________________________
Test Ltd. - Chief Testing Officer
Test Ltd. - Chief Operating Officer
______________________________________________
Tester - Chief Operating and Financial Officer
Testy - Chief Financial Officer
Test 3 - Chief Financial Officer
Test 4 - Chief Financial Officer
Test 5 - Auditor

在我的 doc 模板中,我有一个包含以下 jinja2 标记的表格,可以尝试创建上述内容,但它并不完全有效:

{% hm %}                     | {% for j in item.data %}v                            | {%tc endfor %}
{%tc for item                |{{ j.attributes.company_name }}{{ j.attributes.name }}|
in super_data %}             | {% endfor %}                                         |

也许我需要将列表转换为 python 中的字典才能使其正常工作?不知道该怎么做,如果是这样的话。此外,字典和列表的数量是可变的。 对于如何完成我正在尝试做的事情和使用这个数据集的任何建议,我将不胜感激。我正在拔头发!

【问题讨论】:

    标签: python json python-3.x api jinja2


    【解决方案1】:

    有时先在普通的 python 中做这件事然后让一个 jinja 模板做同样的事情会更容易。 (只需确保您的 super_data 格式正确,因为它缺少一些 "description" 值)

    Python 草案:

    for section in super_data:
        for listing in section["data"]:
            print(listing["attributes"]["company_name"] + " - " + listing["attributes"]["name"])
        print()
    

    草稿结果:

    Test Ltd. - Chief Testing Officer
    Test Ltd. - Chief Operating Officer
    
    Tester - Chief Operating and Financial Officer
    Testy - Chief Financial Officer
    Test 3 - Chief Financial OfficerPartners
    Test4 - Chief Financial Officer
    Test5 - Auditor
    

    Jinja2 模板打印与草稿相同的内容:(确保将命名 super_data 参数传递给 jinja 渲染函数)

    <h1>Jobs</h1>
    {% for section in super_data %}
        {% for listing in section["data"] %}
            <p>
                {{ listing["attributes"]["company_name"] }} - {{ listing["attributes"]["name"] }}
            </p>
        {% endfor %}
        <hr>
    {% endfor %}
    

    渲染的 jinja2 模板:

    【讨论】:

    • 好吧。太感谢了。如果我能不止一次给你投票,我会的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 2021-05-10
    相关资源
    最近更新 更多