[英]Reverse of Python string formatting: generating a dict from a string with named parameters
I have a string like this, where symbol
and property
vary: 我有一个这样的字符串,其中
symbol
和property
有所不同:
a = '/stock/%(symbol)s/%(property)s'
I have another string like this, where AAPL
and price
vary: 我有另一个这样的字符串,其中
AAPL
和price
有所不同:
b = '/stock/AAPL/price'
I'm trying to generate a dict like this: 我正在尝试生成一个像这样的字典:
c = {
'symbol': 'AAPL',
'property': 'price'
}
With string formatting, I could do a this: 使用字符串格式,我可以这样做:
> a % c == b
True
But I'm trying to go the other direction. 但我正在尝试朝另一个方向发展。 Time for some regex magic?
是时候使用一些正则表达式了吗?
A solution with regular expressions: 具有正则表达式的解决方案:
>>> import re
>>> b = '/stock/AAPL/price'
>>> result = re.match('/.*?/(?P<symbol>.*?)/(?P<property>.*)', b)
>>> result.groupdict()
{'symbol': 'AAPL', 'property': 'price'}
You can adjust a bit more the regular expression but, in essence, this is the idea. 您可以更多地调整正则表达式,但从本质上讲,这就是想法。
Assuming well-behaved input, you could just split the strings and zip them to a dict 假设输入行为良好,您可以拆分字符串并将其压缩为字典
keys = ('symbol', 'property')
b = '/stock/AAPL/price'
dict(zip(keys, b.split('/')[2:4]))
This is similar to @moliware's solution, but there's no hard-coding of keys required in this solution: 这类似于@moliware的解决方案,但是此解决方案中不需要对密钥进行硬编码:
import re
class mydict(dict):
def __missing__(self, key):
self.setdefault(key, '')
return ''
def solve(a, b):
dic = mydict()
a % dic
strs = a
for x in dic:
esc = re.escape(x)
strs = re.sub(r'(%\({}\).)'.format(esc), '(?P<{}>.*)'.format(esc), strs)
return re.search(strs, b).groupdict()
if __name__ == '__main__':
a = '/stock/%(symbol)s/%(property)s'
b = '/stock/AAPL/price'
print solve(a, b)
a = "Foo %(bar)s spam %(eggs)s %(python)s"
b = 'Foo BAR spam 10 3.x'
print solve(a, b)
Output: 输出:
{'symbol': 'AAPL', 'property': 'price'}
{'python': '3.x', 'eggs': '10', 'bar': 'BAR'}
As @torek pointed out for cases with ambiguous output(no space between keys) the answer can be wrong here. 正如@torek指出的那样,对于输出含糊(键之间没有空格)的情况,此处的答案可能是错误的。
For eg. 例如。
a = 'leading/%(A)s%(B)s/trailing'
b = 'leading/helloworld/trailing'
Here looking at just b
it's hard to tell the actual value of either either A
or B
. 在这里只看
b
很难说出A
或B
的实际值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.