How to use templating library in python -
i using inbuilt python template library..
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))) $feature_name ").substitute(load_identifier=load_identifier, feature_name=feature_name), zip(load_identifiers, feature_names))
but getting following error:
--------------------------------------------------------------------------- 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))) $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))) $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') 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
op here.. answering question: there '$' in pattern.. needs escaped '$var' used variable substitution. so,
fleh = map(lambda (load_identifier,feature_name):template("flatten((isempty($load_identifier.\$$1) ? null : bagtotuple($load_identifier.\$$1))) $feature_name ").substitute(load_identifier=load_identifier, feature_name=feature_name), zip(load_identifiers, feature_names))
works
Comments
Post a Comment