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))) 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

最佳答案

OP在这里..回答问题: 模式中有一个“$”,需要转义,因为“$var”用于变量替换。 所以,

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))

作品

关于python - 如何在Python中使用模板库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30225909/

相关文章:

Python 正则表达式搜索总是错误的?

python - 从Python模块导入图片

python - 如何让程序根据给定的用户输入再次运行?

python - 如何从命令中识别按钮?

python Pandas : Can you perform multiple operations in a groupby?

python - 在使用 Sphinx 文档时,我应该告诉我的 VCS 忽略哪些文件?

javascript - 智能手机作为服务器

python - 重定向到下载 Django

python - 导入错误 : no module named win32api

c++ - 关于语法错误的 Python 异常文本(boost 库)