[Python-de] str.format() mit loops?

Оlе Ѕtrеісhеr ole-usenet-spam at gmx.net
Di Mai 5 11:09:46 CEST 2015


Hallo,

ich möchte gerne (u.a.) Manpages mit python erstellen, die jeweils ein
festes format erhalten. Gegenwärtig mache ich das etwa so:

-------------------8<---------------------------------------
template = '''NAME
====
{name} - {short_description}

DESCRIPTION
===========
{long_description}

OPTIONS
=======
{options}
'''
option_template = '{option}: {description}'

p1_options = [
    {'option':'-a',
     'description':'do not ignore entries starting with .'},
    {'option':'-l',
     'description':'use a long listing format'},
    ]

p1 = {
    'name': 'ls',
    'short_description': 'list directory contents',
    'long_description': 'List information about ...',
    'options': '\n'.join(option_template.format(**opt) for opt in p1_options)
    }
print template.format(**p1)
-------------------8<---------------------------------------

Das sieht natürlich etwas unübersichtlich aus; insbesondere durch die
mehrstufige Formatierung und die "vielen" (>1) templates. Gibt es hier
eine Möglichkeit, das option_template in das normale template zu
integrieren?

Viele Grüße

Ole


Mehr Informationen über die Mailingliste python-de