2010-05-19 53 views
3

我是Python的新手,所以我不确定这个操作被称为什么,因此我很难在其中搜索信息。如何解析Python中的模板化字符串?

基本上我想有一个字符串,如:

"[[size]] widget that [[verb]] [[noun]]" 

凡大小,动词和名词是每个列表。

我想将字符串解释为元语言,这样我可以从列表中排列很多句子。作为一种元语言,我也可以使其他字符串使用这些预定义的列表来产生更多的排列。

在Python中是否有像这样的变量替换的功能?如果我只是谷歌它,什么术语描述这个操作?

+0

2个月到Python,我回来了这个问题,感到羞怯。看起来像Python现在这样一个基本的方面,但我开始时并不知道Python中的字符串类的第一件事... – 2010-07-08 21:24:39

回答

2

这里是一个可能实现,如果你有sizesverbes,nounes名列表:

import itertools, string 

t = string.Template("$size widget that $verb $noun") 
for size, verb, noun in itertools.product(sizes, verbes, nounes): 
    print t.safe_substitute(size=size, verb=verb, noun=noun) 
1

你想要使用re.sub()或其正则表达式对象与回调函数的方法。

4

如果你改变你的语法

"{size} widget that {verb} {noun}" 

那么你可以使用字符串的format方法做换人:

"{size} widget that {verb} {noun}".format(size='Tiny',verb='pounds',noun='nails') 

choice={'size':'Big', 
    'verb':'plugs', 
    'noun':'holes'} 
"{size} widget that {verb} {noun}".format(**choice) 
1

试试这个脚本:

import random #just needed for the example, not the technique itself 
import re # regular expression module for Python 

template = '[[size]] widget that [[verb]] [[noun]]' 
p = re.compile('(\[\[([a-z]+)\]\])') # match placeholder and the word inside 
matches = p.findall(template) # find all matches in template as a list 

#example values to show you can do substitution 
values = { 
    'size': ('tiny', 'small', 'large'), 
    'verb': ('jumps', 'throws', 'raises'), 
    'noun': ('shark', 'ball', 'roof') 
} 

print 'After each sentence is printed, hit Enter to continue or Ctrl-C to stop.' 

while True: # forever 
    s = template 
    #this loop replaces each placeholder [[word]] with random value based on word 
    for placeholder, key in matches: 
     s = s.replace(placeholder, random.choice(values[key])) 
    print s 
    try: 
     raw_input('') # pause for input 
    except KeyboardInterrupt: #Ctrl-C 
     break # out of loop 

输出示例:

large widget that jumps ball 

small widget that raises ball 

small widget that raises ball 

large widget that jumps ball 

small widget that raises ball 

tiny widget that raises shark 

small widget that jumps ball 

tiny widget that raises shark 
0

正则表达式是矫枉过正。使用循环来设置大小的动词和名词变量然后:

print("%(size)s widget that %(verb)s %(noun)s" % {"size":size, "verb":verb, "noun":noun})