Python centos和windows之间使用jinja2的不同渲染结果
我正在尝试将自定义样式的熊猫数据框输出到html。 我无法理解的一个问题是,当代码在Centos中呈现时,整数呈现为浮点。这不会发生在windows上Python centos和windows之间使用jinja2的不同渲染结果,python,pandas,jinja2,Python,Pandas,Jinja2,我正在尝试将自定义样式的熊猫数据框输出到html。 我无法理解的一个问题是,当代码在Centos中呈现时,整数呈现为浮点。这不会发生在windows上 > /usr/local/lib64/python2.6/site-packages/pandas/core/style.py(267)render() 266 any(any(y) for y in x['props'])] --> 267 d['cellstyle']
> /usr/local/lib64/python2.6/site-packages/pandas/core/style.py(267)render()
266 any(any(y) for y in x['props'])]
--> 267 d['cellstyle'] = trimmed
268 return self.template.render(**d)
ipdb> n
> /usr/local/lib64/python2.6/site-packages/pandas/core/style.py(268)render()
267 d['cellstyle'] = trimmed
--> 268 return self.template.render(**d)
269
ipdb> s
--Call--
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(974)render()
973
--> 974 def render(self, *args, **kwargs):
975 """This method accepts the same arguments as the `dict` constructor:
ipdb> n
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(984)render()
983 """
--> 984 vars = dict(*args, **kwargs)
985 try:
ipdb>
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(985)render()
984 vars = dict(*args, **kwargs)
--> 985 try:
986 return concat(self.root_render_func(self.new_context(vars)))
ipdb>
> /usr/local/lib/python2.6/site-packages/jinja2/environment.py(986)render()
985 try:
--> 986 return concat(self.root_render_func(self.new_context(vars)))
987 except Exception:
ipdb> for i in self.root_render_func(self.new_context(vars)): print i
输出
#T_685db4be_a7ba_11e5_9fb7_0ad4bD99552 th{
文本对齐:居中;
}
#T_685db4be_a7ba_11e5_9fb7_0; d床99552Row0_col0{
文本对齐:右对齐;
}
#T_685db4be_a7ba_11e5_9fb7_0; d床99552Row0_col1{
文本对齐:右对齐;
}
#T_685db4be_a7ba_11e5_9fb7_0; d床99552Row1_col0{
文本对齐:右对齐;
}
#T_685db4be_a7ba_11e5_9fb7_0; d床99552Row1_col1{
文本对齐:右对齐;
}
#T_685db4be_a7ba_11e5_9fb7_0; d床99552Row2_col0{
文本对齐:右对齐;
}
#T_685db4be_a7ba_11e5_9fb7_0; d床99552Row2_col1{
文本对齐:右对齐;
}
身份证件
计数
0
7
2
1
56
2
2
4
3
我找到了一个解决办法
通过检查代码,我发现给定的模板没有正确呈现。圆(精度)
是原因
所以,我对原始模板做了一点修改,以“修复”它。
具体来说,我改变了
{% if c.value is number and c.value|int < c.value %}
{{c.value|round(precision)}}
{% else %}
{{c.value}}
{% endif %}
我仍然不知道为什么不同的平台呈现不同。您在Windows和Linux上使用相同版本的jinja2和Python吗?如果您没有绑定到Python 2.6,我建议您至少更新到最新版本的2.7。我将Python更改为2.7版,并安装了Jinja2 2.8版所需的所有内容。但它仍然不起作用。
from jinja2 import Template
template = Template("""
<style type="text/css" >
{% for s in table_styles %}
#T_{{uuid}} {{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
{% for s in cellstyle %}
#T_{{uuid}}{{s.selector}} {
{% for p,val in s.props %}
{{p}}: {{val}};
{% endfor %}
}
{% endfor %}
</style>
<table id="T_{{uuid}}" {{ table_attributes }}>
{% if caption %}
<caption>{{caption}}</caption>
{% endif %}
<thead>
{% for r in head %}
<tr>
{% for c in r %}
<{{c.type}} class="{{c.class}}">{{c.value}}
{% endfor %}
</tr>
{% endfor %}
</thead>
<tbody>
{% for r in body %}
<tr>
{% for c in r %}
<{{c.type}} id="T_{{uuid}}{{c.id}}" class="{{c.class}}">
{% if c.value is number and c.value|int < c.value %}
{{c.value|round(precision)}}
{% else %}
{{c.value}}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
""")
st = pd.core.style.Styler(v1)
st.template = template
st.render()