Inside a template passed to string.Template
I want to apply a custom format for a datetime object.
The following code does NOT work, but I would like to end up with something like this:
from datetime import datetime as dt
import string
print('{:%a, %d.%m.%Y at %H:%M}'.format(dt.now()))
print(string.Template("${dt:%a, %d.%m.%Y at %H:%M}").substitute({"d": dt.now()}))
The first line will work without any error, but the second will fail with ValueError: Invalid placeholder in string: line 1, col 1
CodePudding user response:
>>> print(string.Template("{$d:%a, %d.%m.%Y at %H:%M}").substitute({"d": dt.now()}))
{2022-04-19 10:51:57.409244:%a, %d.%m.%Y at %H:%M}
i got past the invalid placeholder error by moving $ to d. but the formatting is not as expected