def extend(format,d,maker=‘"‘,safe=True):
if safe:
def lookup(w): return d.get(w,w.join(maker*2))
else:
def lookup(w): return d[w]
parts=format.split(maker)
print parts
parts[1::2]=map(lookup, parts[1::2])
print parts
return ‘ ‘.join(parts)
if __name__==‘__main__‘:
print extend(‘just "a" test‘,{‘b‘:‘one‘})
原文地址:http://riyue.blog.51cto.com/8572160/1682611