如何使用特定的输出格式在python中将矩阵(从一个文件读取)写入另一个csv文件

时间:2016-04-01 14:35:47

标签: python csv matrix file-writing

我有一个csv文件,其中包含矩阵格式的数据,其样本如下所示:

index,col1,col2,col3,col4,col5,col6
col1_1,1,0.005744233,0.013118052,-0.003772589,0.004284689
col2_1,0.005744233,1,-0.013269414,-0.007132092,0.013950261
col3_1,0.013118052,-0.013269414,1,-0.014029249,-0.00199437
col4_1,-0.003772589,-0.007132092,-0.014029249,1,0.022569309
col5_1,0.004284689,0.013950261,-0.00199437,0.022569309,1

不,我想读取此文件中的数据并将其写入另一个csv文件,但我需要的格式为:

col1_1,value,col1
col1_1,value,col2
col1_1,value,col3
.
.
.
col2_1,value,col1
col2_1,value,col2
.
.
.

所以基本上第一个元素是第一列中的列名,后面是第一行中该列和元素的值。

我编写了这段代码,但它只是写错了格式:

reader = csv.reader(open(IN_FILE, "r"), delimiter=',')
writer = csv.writer(open(OUT_FILE, "w"), delimiter=',')

with open(IN_FILE) as infile:
    with open(OUT_FILE, "w") as outfile:
        reader = csv.reader(infile, delimiter=",")
        writer = csv.writer(outfile, delimiter=",")
        writer.writerow(next(reader))
        for line in reader:
            writer.writerow([line[0],line[1]])

我怎么能在python中做到这一点?

2 个答案:

答案 0 :(得分:1)

试试这个:

a+b

这似乎有效。虽然您的测试数据看起来缺少了#col;'

初始代码的问题在于它没有遍历每列的行。

答案 1 :(得分:0)

如果您的文件包含我假设的列和行索引,则应该这样做。

old_data = reader
new_data = []

for row in xrange(0,len(old_data)):
    for col in xrange(0,len(row)):
        if (not row == 0 and not col == 0):
            new_data.append([old_data[row][0],old_data[row][col],old_data[0][col]])

writer.writerows(new_data)
csv_file.close()