python: convert old %-formatting style to "{}".format() style
by kaz2100 from LinuxQuestions.org on (#5G394)
Can python experts please help me?
I am converting "%s" % style code to modern "{}".format() style.
Original code is:Code:sws = ["a", "an" ...]
... "|".join([r"%s\b" % w for w in sws])Red part needs to be rewritten. Simple replacement likeCode:"|".join([r"{}\b".format(w for w in sws)])does not work.
It looks like thatCode:"|".join([w + r"\b" for w in sws]) may work. But it is violating Python philosophy. It is UGRY! and COMPLEX compared to original beautiful code. Also, it is a violation of "there should be one- and preferably only one -obvious way to do it" rule.
To avoid unnecessary trouble in future, python way should be sought.
How to avoid these violations?
cheers


I am converting "%s" % style code to modern "{}".format() style.
Original code is:Code:sws = ["a", "an" ...]
... "|".join([r"%s\b" % w for w in sws])Red part needs to be rewritten. Simple replacement likeCode:"|".join([r"{}\b".format(w for w in sws)])does not work.
It looks like thatCode:"|".join([w + r"\b" for w in sws]) may work. But it is violating Python philosophy. It is UGRY! and COMPLEX compared to original beautiful code. Also, it is a violation of "there should be one- and preferably only one -obvious way to do it" rule.
To avoid unnecessary trouble in future, python way should be sought.
How to avoid these violations?
cheers