gpt4 book ai didi

Python:在写入 csv 文件时避免列的覆盖/不可读

转载 作者:太空宇宙 更新时间:2023-11-03 17:16:34 28 4
gpt4 key购买 nike

在 Python 2.7 中,我尝试按照以下方式编写 csv 文件:

#do stuff
import csv
with open(newpath1+'\\stats_matrix_LATTICE_NETWORK.csv', 'wb') as f: #Change file name as needed
writer = csv.writer(f, delimiter='\t', quoting=csv.QUOTE_NONE)
writer.writerows(stats_matrix) #stats_matrix is my list with the values

我得到了这样的可视化效果:

dict1   dict2   dict3   dict4   dict5   dict6   dict7   dict8
8904 89.04 1096 10.959999999999994 39 0.39 8943 89.43 1057 99.61 8943 89.43 1057 10.57 1014 1014 0.0 3.7369914853358561
9358 93.58 642 6.420000000000002 43 0.43 9401 94.01 599 99.57 9401 94.01 599 5.99 0 0 0 0.0
9600 96.0 400 4.0 13 0.13 9613 96.13 387 99.87 9613 96.13 387 3.87 0 0 0 0.0
7595 75.95 2405 24.049999999999997 70 0.7 7665 76.65 2335 99.3 7665 76.65 2335 23.35 0 0 0 0.0

虽然我使用 delimiter='\t',但我的列不可读。 有没有办法让我放置额外的选项卡以便更有效地分隔列?

最佳答案

根据您用来查看文件的编辑器,您会看到它的显示方式有所不同,具体取决于它处理选项卡的方式。

与其尝试使用 CSV 格式(逗号分隔变量),为什么不使用空格填充列呢?毕竟,记事本是为了查看文本文件而设计的。只要每一行包含相同数量的列,就可以使用以下函数自动对齐每一列:

data = [
["dict1", "dict2", "dict3", "dict4", "dict5", "dict6", "dict7", "dict8", "dict9", "dict10", "dict11", "dict12", "dict13", "dict14", "dict15", "dict16", "dict17", "dict18"],
["8904", "89.04", "1096", "10.959999999999994", "39", "0.39", "8943", "89.43", "1057", "99.61", "8943", "89.43", "1057", "10.57", "1014", "1014", "0.0", "3.7369914853358561"],
["9358", "93.58", "642", "6.420000000000002", "43", "0.43", "9401", "94.01", "599", "99.57", "9401", "94.01", "599", "5.99", "0", "0", "0", "0.0"],
["9600", "96.0", "400", "4.0", "13", "0.13", "9613", "96.13", "387", "99.87", "9613", "96.13", "387", "3.87", "0", "0", "0", "0.0"],
["7595", "75.95", "2405", "24.049999999999997", "70", "0.7", "7665", "76.65", "2335", "99.3", "7665", "76.65", "2335", "23.35", "0", "0", "0", "0.0"],
]

def write_cols(filename, data):
widths = [0] * len(data[0])

for row in data:
widths[:] = [max(widths[index], len(str(col))) for index, col in enumerate(row)]

with open(filename, 'w') as f_output:
for row in data:
f_output.write(" ".join("%-*s" % (widths[index], col) for index, col in enumerate(row)) + '\n')

write_cols('output.txt', data)

对于您的数据,它将被写入一个文本文件,该文件将在记事本中显示如下:

dict1  dict2  dict3  dict4               dict5  dict6  dict7  dict8  dict9  dict10  dict11  dict12  dict13  dict14  dict15  dict16  dict17  dict18            
8904 89.04 1096 10.959999999999994 39 0.39 8943 89.43 1057 99.61 8943 89.43 1057 10.57 1014 1014 0.0 3.7369914853358561
9358 93.58 642 6.420000000000002 43 0.43 9401 94.01 599 99.57 9401 94.01 599 5.99 0 0 0 0.0
9600 96.0 400 4.0 13 0.13 9613 96.13 387 99.87 9613 96.13 387 3.87 0 0 0 0.0
7595 75.95 2405 24.049999999999997 70 0.7 7665 76.65 2335 99.3 7665 76.65 2335 23.35 0 0 0 0.0

这假设您的编辑器使用固定宽度字体。

关于Python:在写入 csv 文件时避免列的覆盖/不可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33613392/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com