"%(a)s ... %(b)s" % {'a': foo, 'b': '%(b)s'}
Darrell
darrell at dorb.com
Sat Dec 4 14:05:07 EST 1999
import re
MAIL = '''From: someone <%(email)s>
Subject: test
this is feedback.py %(version)s
'''
dic={'version': "1.1"}
def format(fmt, dic):
converted=[]
nonConverted=[]
def func(mo, dic=dic, conv=converted, nonConv=nonConverted):
k,t=mo.groups()
if dic.has_key(k):
conv.append(k)
return mo.group(0)%dic
nonConv.append(k)
return mo.group(0)
return re.compile('%\((.*?)\)(\w)').sub(func,fmt), converted,
nonConverted
result=format(MAIL, dic)
print result[0]
print 'Converted:', result[1]
print 'Not converted:', result[2]
######### output
From: someone <%(email)s>
Subject: test
this is feedback.py 1.1
Converted: ['version']
Not converted: ['email']
--Darrell
----- Original Message -----
From: "Gerrit Holl" <gerrit.holl at pobox.com>
>
> This is ugly! Isn't there a better way to fill in just SOME of the %(...)s
> values in a string and leave the rest as is?
>
More information about the Python-list
mailing list