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

Python使用tabulate写入CSV时数据集中在单列的解决方法

问题现象
  • 使用tabulate库生成表格内容写入CSV文件后,用Excel打开所有数据全部集中在单个列中
  • 尝试使用Excel自带的「文本分列」功能,无法按预期正确拆分各列
  • 问题复现代码:
data = [['Zone1', z1_results[0], z1_results[1], z1_results[2], z1_results[3],
     z1_results[4], z1_results[5]],
    ['Zone2', z2_results[0], z2_results[1], z2_results[2], z2_results[3],
     z2_results[4], z2_results[5]],
    ['Zone3', z3_results[0], z3_results[1], z3_results[2], z3_results[3],
     z3_results[4], z3_results[5]],
    ['Zone4', z4_results[0], z4_results[1], z4_results[2], z4_results[3],
     z4_results[4], z4_results[5]],
    ['Zone5', z5_results[0], z5_results[1], z5_results[2], z5_results[3],
     z5_results[4], z5_results[5]]]

head = ['Zone', 'M1Min (kN.m)', 'M1Max (kN.m)', 'M2Min (kN.m)', 'M2Max (kN.m)', 'M3Min (kN.m)', 'M3Max (kN.m)']

Table = tabulate(data, headers=head, tablefmt="plain")
print(tabulate(data, headers=head, tablefmt="fancy_grid"))

text_file = open("Table_summary.csv", "w", encoding='utf-8')
text_file.write(Table)
text_file.close()
  • 问题截图:
    CSV打开后数据单列显示
问题原因

代码中使用tablefmt="plain"生成的是靠空格对齐排版的纯文本内容,不是CSV标准要求的、以固定分隔符(逗号/制表符等)分隔字段的结构化内容。Excel打开CSV时会按分隔符识别列边界,识别不到合法分隔符时就会把整行内容归入同一列,这也是手动文本分列无法正确拆分的原因——列之间的空格数量是tabulate为了对齐自动填充的,不是固定数量,无法作为稳定的分列依据。

解决方案

方案1(推荐):使用Python标准库csv写入标准CSV

csv库是Python内置模块,无需额外安装,生成的CSV完全符合格式规范,可直接被Excel、WPS等工具正确识别,同时可以保留tabulate用于控制台美化打印的逻辑:

import csv
from tabulate import tabulate

# 原有data、head定义保持不变
data = [['Zone1', z1_results[0], z1_results[1], z1_results[2], z1_results[3],
     z1_results[4], z1_results[5]],
    ['Zone2', z2_results[0], z2_results[1], z2_results[2], z2_results[3],
     z2_results[4], z2_results[5]],
    ['Zone3', z3_results[0], z3_results[1], z3_results[2], z3_results[3],
     z3_results[4], z3_results[5]],
    ['Zone4', z4_results[0], z4_results[1], z4_results[2], z4_results[3],
     z4_results[4], z4_results[5]],
    ['Zone5', z5_results[0], z5_results[1], z5_results[2], z5_results[3],
     z5_results[4], z5_results[5]]]

head = ['Zone', 'M1Min (kN.m)', 'M1Max (kN.m)', 'M2Min (kN.m)', 'M2Max (kN.m)', 'M3Min (kN.m)', 'M3Max (kN.m)']

# 控制台打印美化表格,保留原有效果
print(tabulate(data, headers=head, tablefmt="fancy_grid"))

# 写入标准CSV,使用utf-8-sig编码避免Windows下Excel打开乱码
with open("Table_summary.csv", "w", encoding='utf-8-sig', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(head)
    writer.writerows(data)

方案2:使用tabulate内置的CSV输出格式

如果要沿用tabulate生成文件内容,只需要将tablefmt参数改为csv,即可生成符合CSV规范的逗号分隔内容:

from tabulate import tabulate

# 原有data、head定义保持不变
# 生成CSV格式内容,不要使用plain/fancy_grid等展示类格式
Table = tabulate(data, headers=head, tablefmt="csv")
print(tabulate(data, headers=head, tablefmt="fancy_grid"))

with open("Table_summary.csv", "w", encoding='utf-8-sig') as text_file:
    text_file.write(Table)

注意:plain、fancy_grid、grid这类tablefmt参数是为终端/文本展示设计的,输出内容带排版用的填充空格、边框字符,不能用于生成结构化数据文件。


内容的提问来源于stack exchange,提问作者Omkar S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 01:09:25