连接 - 添加而不是追加

时间:2015-10-31 15:59:29

标签: python

我正在运行以下python文件,该文件应该将来自多个文件夹的文件内容连接到一个带有公共头文件的csv。文件的内容正在根据需要连接,但结果正在添加,而不是追加。

这是我得到的结果的一个例子:

enter image description here

这是我需要的一个例子:

enter image description here

这是python文件的代码,为清楚起见删除了变量:

import sys
import re
import os

frequencyCount = {}

with open(sys.argv[1] + '/Concatenated.csv', 'w+') as outfile:
try:
    with open(sys.argv[1] + '/MatrixHeader.csv') as headerfile:
        for line in headerfile:
            outfile.write(line + '\n')
except:
    print 'No Header File'


                    //vars were in here
                    //built columnTuple

                    if url in frequencyCount:
                        frequencyCount[url] = tuple(sum(t) for t in zip(frequencyCount[url], columnTuple))
                    else:
                        frequencyCount[url] = columnTuple

# write the tuples to stdout
# Note: they are unsorted
for url in frequencyCount.keys():
    writeString = url
    for col in frequencyCount[url]:
        writeString = writeString + "," + str(col)
    writeString = writeString + "\n"
    outfile.write(writeString)

1 个答案:

答案 0 :(得分:1)

是不是因为你在sum使用的tuple(sum(t) for t in zip(frequencyCount[url], columnTuple))?我认为你应该摆脱对sum的呼吁。