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-字符串到插值 - 提供 - 预期的占位符,蟒蛇 –