Is there a better way of doing the following..
template = """
This goes here {Item1},,, and here {Item2}
and here {Item3}
"""
mydict = {'Item1':'Cat',
'Item2':'Dog',
'Item3':'Hamster',
'Item4':'Donkey'
}
result = template.format(Item1=mydict['Item1'], Item2=mydict['Item2'], Item3=mydict['Item3'])
The main thing being that the place holders in the format string are named exactly the same as the key values in the dictionary.
BUT The dictionary may contain more key values than there are place holders. The place holders will ALWAYS be a subset of the dictionary
I have tried
template.format(**mydict)
but that gives a KeyError exception. I had hoped that silently catching that exception would work but it doesn't
My gut feeling is that there must be a more pythonic way to do this other than the long-hand way above.
Thanks