【问题标题】:when use python mako how to return other type data使用 python mako 时如何返回其他类型的数据
【发布时间】:2020-07-14 01:26:34
【问题描述】:
from mako.template import Template

t=Template("${x}").render(x=[1,2,3])
print(type(t))   # <class 'str'>
print(t)         # [1, 2, 3]

t=Template("${x}").render(x=5)
print(type(t))  #<class 'str'>
print(t)        # 5

为什么return不是list或者int,我想得到什么

我可以用吗?,但是我很失望

t=Template("${int(x)}").render(x=5)
print(type(t))   #<class 'str'>
print(t)         #5

【问题讨论】:

    标签: python mako


    【解决方案1】:

    您可以使用

    将类型转换为所需的数据类型

    t = int(Template("${int(x)}").render(x=5))

    t = [Template("${int(x)}").render(x=5)]

    【讨论】:

    • ['[1, 2, 3]'] 5
    【解决方案2】:

    感谢您的建议,但这不是我想要的。我传入的数据类型未知

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-22
      • 2020-07-11
      • 1970-01-01
      • 2014-04-03
      • 1970-01-01
      • 2018-09-10
      • 2022-01-26
      • 1970-01-01
      相关资源
      最近更新 更多