简体   繁体   中英

Render list in ordered structure without unicode characters

I have a list that looks like this:

[(u'Element1', u'Description1', u'Status1), (u'newElement2', u'newDescription2', u'newStatus2), (u'nextElement3', u'nextDescription3', u'nextStatus3), (u'anotherElement4', u'anotherDescription4', u'anotherStatus4)] 

I have an ansible playbook that uses a jinja2 template to render the list to a text file. The template file looks like this:

{% for item in description | batch(3, ' ') %}
{% for el in item %}
{{ el }}
{% endfor %}
{% endfor %}

But this returns the text file to look like this:

[u'Element1', u'Description1', u'Status1]
[u'newElement2', u'newDescription2', u'newStatus2]
[u'nextElement3', u'nextDescription3', u'nextStatus3]
[u'anotherElement4', u'anotherDescription4', u'anotherStatus4]

What I want the report to look like is this:

Element1           Description1           Status1
newElement2        nextDescription2       newStatus2
nextElement3       nextDescription3       nextStatus3
anotherElement4    anotherDescription4    anotherDescription4

Is there a way to remove the unicode characters and render the lists this way?

You could try

{% for el in item %}
    {% for e in el %}
        {{ e }}
    {% endfor %}
{% endfor %}

Or use html tables if you want to be able to change formatting

For example:

{% for row in description %}
{% for cell in row %}
{{ "%-22s"|format(cell) }} 
{%- endfor %}

{% endfor %}

Yields:

Element1              Description1          Status1
newElement2           newDescription2       newStatus2
nextElement3          nextDescription3      nextStatus3
anotherElement4       anotherDescription4   anotherStatus4

But to get a dynamic padding - depending on a max length of an element in a column - looks like a much more complex task: {{ "%-22s"|format(cell) }} can be replaced with {{ "{0}".format(cell).ljust(width) }} where width can be a variable, but likely it would require another loop first to collect the lengths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM