使用python template,我可以生成输出。喜欢

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'


如果我有字符串

'tim likes kung pao'


如何在单独的变量中获取字符串timkung pao

最佳答案

一种方法是使用regular expressions

In [8]: import re

In [9]: who, what = re.match(r'(.*) likes (.*)', 'tim likes kung pao').groups()

In [10]: who
Out[10]: 'tim'

In [11]: what
Out[11]: 'kung pao'

关于python - Python逆模板,从字符串获取变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18659844/

10-09 19:59