Python:写入 csv 文件时避免 overwriting/unreadability 列

Python: Avoiding overwriting/unreadability of columns while writing a csv file

在 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',但我的专栏无法阅读。 有没有办法让我添加额外的 tabs 以便更有效地分隔列?

根据您查看文件所用的编辑器,您会看到文件的显示方式因它处理制表符的方式而异。

与其尝试使用 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 

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