Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

json - How to work with nested list of dicts for docxtpl jinja2 tags in python

I have a list that contains dictionaries of lists that contain dictionaries of lists which contain dictionaries of key/value pairs. It looks something like this:

super_data: [{'data': [{'attributes': {'company_name': 'Test Ltd.',
                           'created_at': '2011-03-31T22:24:37.000Z',
                           'description': 'Department: Testing Department
'
                                          'Salary: 100000
'
                                          'Bonus: 25000
'
                                          'Position Type: Full Time
'
                                          'Reference: 0
'
                                          'Company Confirmed: 0
'
                                          'Candidate Confirmed: 0
'
                                          'Signing: 0
'
                                          'Performance: -1
'
                                          'Car: -1
'
                                          'Stock: -1
'
                                          'Club: -1
'
                                          'Performance Details: 50%
',
                           '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
',
                           '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}}]

I'm trying to use this to place specific values in cells in a word template using docxtpl and jinja2 tags.

For example: I want to be able to have the first cell in a column contain the values for all of the 'company_name' keys and 'name' keys inside all of the 'attribute' sets of the first 'data' set, and then have the next ones in subsequent cells in a column.

So I'm trying to make it look like this:

                 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

In my doc template I have a table containing the following jinja2 tags to try create the above, but it's not quite working:

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

Maybe I need to convert the lists to dicts in python to get this to work? Not sure how to do that, if that's the case. Also, the number of dicts and lists is variable. Would appreciate any advice on how to accomplish what I'm trying to do and working with this data set.. I'm pulling my hair out!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Sometimes it is easier to first do it in normal python then make a jinja template do the same thing. (Just make sure that your super_data is correctly formatted as it was missing some "description" values)

Python draft:

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

Draft result:

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 template to print same thing as the draft: (make sure to pass named super_data argument to jinja rendering function)

<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 %}

Rendered jinja2 template:

Resulting html from jinja2 template


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...