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())
添加回答
舉報