2013-08-20 29 views

回答

0

使用regex

>>> import re 
>>> a = "test string with %(experiment1)s and %(experiment2)s" 
>>> re.findall(r'%\((.*?)\)', a) 
['experiment1', 'experiment2'] 
+0

的感謝!它完美的作品! –

4

您也可以欺騙Python的格式化程序爲尋找的鑰匙給你:

class MyDict(dict): 
    def __missing__(self, key): 
     return self.setdefault(key, "") 

d = MyDict() 
dummy = "test string with %(experiment1)s and %(experiment2)s" % d 
print d.keys() 

打印

['experiment1', 'experiment2'] 
+0

+1學到了新東西。 –

+0

+1非常有趣的解決方案!謝謝! –

相關問題