1 回答

TA贡献1825条经验 获得超4个赞
您可以使用 . 在循环内创建文件groupby()。这是一个例子:
tmpl = env.from_string("""
<div>
{{ df.to_html(index=False) }}
</div>
""")
for color_name, group_df in df.groupby(['Color']):
content = tmpl.render(df=group_df)
file_path = '/tmp/{f_name}.html'.format(f_name=color_name)
with open(file_path, 'w+') as file:
print('writing to file {f}'.format(f=file_path))
# print(content) # check content before write if you need
file.write(content)
# check content after write if you need
# with open(file_path) as file:
# print('reading file {f}. content:'.format(f=file_path))
# print(file.read())
添加回答
举报