如何在python中使用模板库
我正在使用内置的python模板库如何在python中使用模板库,python,Python,我正在使用内置的python模板库 from string import Template feature_names = ['f1','f2'] load_identifiers = ["foo","bar"] fleh = map(lambda (load_identifier,feature_name):Template("FLATTEN((IsEmpty($load_identifier.\$1) ? null : BagToTuple($load_identifier.\$1))) A
from string import Template
feature_names = ['f1','f2']
load_identifiers = ["foo","bar"]
fleh = map(lambda (load_identifier,feature_name):Template("FLATTEN((IsEmpty($load_identifier.\$1) ? null : BagToTuple($load_identifier.\$1))) AS $feature_name ").substitute(load_identifier=load_identifier, feature_name=feature_name), zip(load_identifiers, feature_names))
但我得到了以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-177-ddbfd32ef884> in <module>()
----> 1 fleh = map(lambda (load_identifier,feature_name):Template("FLATTEN((IsEmpty($load_identifier.\$1) ? null : BagToTuple($load_identifier.\$1))) AS $feature_name ").substitute(load_identifier=load_identifier, feature_name=feature_name), zip(load_identifiers, feature_names))
<ipython-input-177-ddbfd32ef884> in <lambda>((load_identifier, feature_name))
----> 1 fleh = map(lambda (load_identifier,feature_name):Template("FLATTEN((IsEmpty($load_identifier.\$1) ? null : BagToTuple($load_identifier.\$1))) AS $feature_name ").substitute(load_identifier=load_identifier, feature_name=feature_name), zip(load_identifiers, feature_names))
/anaconda/lib/python2.7/string.pyc in substitute(self, *args, **kws)
170 raise ValueError('Unrecognized named group in pattern',
171 self.pattern)
--> 172 return self.pattern.sub(convert, self.template)
173
174 def safe_substitute(self, *args, **kws):
/anaconda/lib/python2.7/string.pyc in convert(mo)
167 return self.delimiter
168 if mo.group('invalid') is not None:
--> 169 self._invalid(mo)
170 raise ValueError('Unrecognized named group in pattern',
171 self.pattern)
/anaconda/lib/python2.7/string.pyc in _invalid(self, mo)
144 lineno = len(lines)
145 raise ValueError('Invalid placeholder in string: line %d, col %d' %
--> 146 (lineno, colno))
147
148 def substitute(self, *args, **kws):
ValueError: Invalid placeholder in string: line 1, col 36
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
---->1 fleh=map(lambda(加载标识符,特征名称):模板(“flatte((IsEmpty($加载标识符。\$1)?null:BagToTuple($加载标识符。\$1)))作为$feature\u name”)。替换(加载标识符=加载标识符,特征名称=特征名称),zip(加载标识符,特征名称))
输入((加载\标识符、特征\名称))
---->1 fleh=map(lambda(加载标识符,特征名称):模板(“flatte((IsEmpty($加载标识符。\$1)?null:BagToTuple($加载标识符。\$1)))作为$feature\u name”)。替换(加载标识符=加载标识符,特征名称=特征名称),zip(加载标识符,特征名称))
/anaconda/lib/python2.7/string.pyc替换为(self,*args,**kws)
170 raise VALUERROR('模式中无法识别的命名组',
171.自我模式)
-->172返回self.pattern.sub(转换,self.template)
173
174 def安全_替代品(自身,*args,**kws):
/转换中的anaconda/lib/python2.7/string.pyc(mo)
167返回自分隔符
168如果mo.group(“无效”)不是无:
-->169自失效(mo)
170 raise VALUERROR('模式中无法识别的命名组',
171.自我模式)
/anaconda/lib/python2.7/string.pyc in_无效(self,mo)
144行编号=长度(行)
145 raise VALUERROR('字符串中的占位符无效:第%d行,第%d列'%
-->146(lineno,colno))
147
148 def替代品(自身,*args,**kws):
ValueError:字符串中的占位符无效:第1行,第36列
请点击此处。。回答问题:
模式中有一个“$”。。它需要转义,因为“$var”用于变量替换。
所以
工作无需导入模板
feature_names = ['f1','f2']
load_identifiers = ["foo","bar"]
ts = "FLATTEN((IsEmpty(%(load_identifier)s.$1) ? null : BagToTuple(%(load_identifier)s.$1))) AS %(feature_name)s"
fleh = [ts % d for d in [{'feature_name': fn, 'load_identifier': li}
for fn, li in zip(feature_names, load_identifiers)]]
你不需要反斜杠<代码>$$1就足够了。
feature_names = ['f1','f2']
load_identifiers = ["foo","bar"]
ts = "FLATTEN((IsEmpty(%(load_identifier)s.$1) ? null : BagToTuple(%(load_identifier)s.$1))) AS %(feature_name)s"
fleh = [ts % d for d in [{'feature_name': fn, 'load_identifier': li}
for fn, li in zip(feature_names, load_identifiers)]]