Python:避免在编写csv文件时覆盖/无法读取列

Python:避免在编写csv文件时覆盖/无法读取列,python,csv,tabs,file-writing,Python,Csv,Tabs,File Writing,在Python2.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

在Python2.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

虽然我使用了
分隔符='\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 

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

如何获得视觉效果?您是否能够使用Excel或Google Sheets之类的工具来查看内容并提供数据格式,这样您就不需要插入额外的制表符?您所说的“如何获得视觉效果”是什么意思?我只需要得到一个
csv
文件,然后用记事本打开它。如果我能够在不重叠的情况下读取值,那会更有帮助。。。