You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用tabulate包将列表内容垂直打印而非水平显示?

实现列表项垂直显示的解决方案

修改你的代码,将每个价格单独作为一行数据传入tabulate即可,具体代码如下:

def extract_data_excel_combined(self):
        dict_fruit = {"Watermeloen": 3588.20, "Appel": 5018.75, "Sinaasappel": 3488.16}
        fruit_list = list(dict_fruit.values())
        # 每个价格单独封装为单元素列表,作为表格的一行
        new_fruit_list = [[value] for value in fruit_list]    
        columns = ["totaal kosten fruit"]

        return mark_safe(
            tabulate(new_fruit_list, headers=columns, tablefmt="html", stralign="center")
        )

改动说明

原代码中你将所有价格放在同一个列表里添加到new_fruit_list,tabulate会把这个列表解析为一行中的多列数据,因此呈现横向排列。现在通过列表推导式,把每个价格单独封装成单元素列表,new_fruit_list就变成了多行数据的集合,tabulate会逐行渲染,最终实现垂直显示的效果。

内容的提问来源于stack exchange,提问作者mightycode Newton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 01:05:27