Python附加列标题&将列中的列值附加到csv

时间:2016-09-28 18:37:04

标签: python list loops csv parsing

我正在尝试追加列标题(硬编码)并将列中的列值附加到现有的csv。我没有得到理想的结果。

方法1将结果附加到现有的csv文件中。方法2将现有csv的副本克隆到temp.csv中。这两种方法都没有让我得到我想要的输出。在结果1中,它只是在最后一行单元格后附加。在结果2中,所有列表值都附加在每一行上。预期的结果是我正在寻找的。

我在下面提供了我的代码。感谢任何输入或指导。

现有CSV Test.csv

Type,Id,TypeId,CalcValues
B,111K,111Kequity(long) 111K,116.211768
C,111N,B(long) 111N,0.106559957
B,111J,c(long) 111J,20.061634

代码 - 方法1& 2

final_results = ['0.1065599566767107', '0.0038113334533441123', '20.061623176440904']

# Method1
csvfile = "test.csv"
with open(csvfile, "a") as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in final_results:
        writer.writerow([val])  

# Method2
with open("test.csv", 'rb') as input, open('temp.csv', 'wb') as output:
    reader = csv.reader(input, delimiter = ',')
    writer = csv.writer(output, delimiter = ',')

    all = []
    row = next(reader)
    row.insert(5, 'Results')
    all.append(row)

    for row in reader:
        for i in final_results:
            print type(i)
            row.insert(5, i)
        all.append(row)
    writer.writerows(all)

方法1的结果

Type,Id,TypeId,CalcValues
B,111K,111Kequity(long) 111K,116.211768
C,111N,B(long) 111N,0.106559957
B,111J,c(long) 111J,20.0616340.1065599566767107
0.0038113334533441123
20.061623176440904

方法2的结果

Type,Id,TypeId,CalcValues,Results
B,111K,111Kequity(long) 111K,116.211768,0.1065599566767107,20.061623176440904,0.0038113334533441123
C,111N,B(long) 111N,0.106559957,0.1065599566767107,20.061623176440904,0.0038113334533441123
B,111J,c(long) 111J,20.061634,0.1065599566767107,20.061623176440904,0.0038113334533441123

预期结果

Type,Id,TypeId,CalcValues,ID
B,111K,111Kequity(long) 111K,116.211768,0.1065599566767107
C,111N,B(long) 111N,0.106559957,20.061623176440904
B,111J,c(long) 111J,20.061634,0.0038113334533441123

2 个答案:

答案 0 :(得分:1)

第一种方法必然会失败:您不想添加新行而是添加新列。所以回到第二种方法:

您插入标题确定,但是您在每行上循环搜索结果,而您需要迭代它们。

为此,我从final_results列表(带__iter__())创建一个迭代器,然后我调用it.next并附加到每一行(不需要插入到最后,只需追加)

我删除了all大列表,因为1)您可以一次写一行,节省内存,2)all是预定义的函数。避免将其用作变量。

final_results = ['0.1065599566767107', '0.0038113334533441123', '20.061623176440904']

# Method2
with open("test.csv", 'rb') as input, open('temp.csv', 'wb') as output:
    reader = csv.reader(input, delimiter = ',')
    writer = csv.writer(output, delimiter = ',')


    row = next(reader)  # read title line
    row.append("Results")
    writer.writerow(row)  # write enhanced title line

    it = final_results.__iter__()  # create an iterator on the result

    for row in reader:
        if row:  # avoid empty lines that usually lurk undetected at the end of the files
            try:
                row.append(next(it))  # add a result to current row
            except StopIteration:
                row.append("N/A")     # not enough results: pad with N/A
            writer.writerow(row)

结果:

Type,Id,TypeId,CalcValues,Results
B,111K,111Kequity(long) 111K,116.211768,0.1065599566767107
C,111N,B(long) 111N,0.106559957,0.0038113334533441123
B,111J,c(long) 111J,20.061634,20.061623176440904

注意:如果我们在"Results"变量中包含final_results,我们甚至不需要以不同的方式处理第一行。

注意2:值似乎错误:final_results似乎与预期输出的顺序不同。 Result列已转到ID,但这很容易纠正。

答案 1 :(得分:1)

import csv

HEADER = "Type,Id,TypeId,CalcValues,ID"
final_results = ['0.1065599566767107', '20.061623176440904', '0.0038113334533441123']

with open("test.csv") as inputs, open("tmp.csv", "wb") as outputs:
    reader = csv.reader(inputs, delimiter=",")
    writer = csv.writer(outputs, delimiter=",")

    reader.next()  # ignore header line
    writer.writerow(HEADER.split(","))  

    for row in reader:
        writer.writerow(row + [final_results.pop(0)])

我将标题字段存储到HEADER并切换final_results的第2和第3个元素,使用pop(0)删除并返回final_results的第一个元素

输出:

Type,Id,TypeId,CalcValues,ID
B,111K,111Kequity(long) 111K,116.211768,0.1065599566767107
C,111N,B(long) 111N,0.106559957,20.061623176440904
B,111J,c(long) 111J,20.061634,0.0038113334533441123