0
我想將期望參數字典的字符串轉換爲預期字典的鍵列表,例如找到f這樣的:Python:獲取字符串參數
f("some text %(foo)s %(bar)s") == ['foo', 'bar',] # True
有沒有辦法做到這一點?
我想將期望參數字典的字符串轉換爲預期字典的鍵列表,例如找到f這樣的:Python:獲取字符串參數
f("some text %(foo)s %(bar)s") == ['foo', 'bar',] # True
有沒有辦法做到這一點?
如何:
>>> S = "some text %(foo)s %(bar)s"
>>> print re.findall(r'%\((.*?)\)', S)
['foo', 'bar']
SMTH像
>>> import re
>>> re.findall("%\(([^\)]+)\)[sif]", "some text %(foo)s %(bar)s", re.M)
['foo', 'bar']
[sif]
部分可以使用下表符號上http://docs.python.org/library/stdtypes.html#string-formatting-operations
擴展http://stackoverflow.com/questions/4839121/檢查-IF-A-字符串到插值 - 提供 - 預期的佔位符,蟒蛇 –