Home Render list in ordered structure without unicode characters
Reply: 2

Render list in ordered structure without unicode characters

CEamonn
1#
CEamonn Published in 2017-12-06 11:13:07Z

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?

techraf
2#
techraf Reply to 2017-12-06 12:40:34Z

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.

maxou
3#
maxou Reply to 2017-12-06 11:16:31Z

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.411961 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO