指定列以在python中的csv文件中输入数据

时间:2014-02-17 17:30:34

标签: python csv

我是python的新手。因此,我非常感谢有关在CSV文件中处理数据的问题的一些建议。 (1)在下面的代码中,如何在CSV文件中指定要插入第9列和第10列的Data1和Data2? (2)另外,为了将来参考,如何指定行和列来提取数据或写入csv文件?

file1  = open (fileA.csv, 'rb')
reader1 = csv.reader(file1)

outfile = open ('fileB.csv', 'wb')
writer = csv.writer(outfile)

next(reader1, None)  # skip the headers

for col in reader1:
    subset1 = [float(x) for x in col[3:6]]
    Max1 = max (subset1)
    Min1 = min (subset1)
    Data1 = str(Max1 - Min1)
    subset2 = [float(x) for x in col[6:9]]
    Max2 = max (subset2)
    Min2 = min (subset2)
    Data2 = str(Max2 - Min2)
    ........????            

    writer.writerow(Data)


 file1.close()
 outfile.close()

非常感谢!

1 个答案:

答案 0 :(得分:3)

简单地做

writer.writerow(col[:9] + [Data1, Data2] + col[9:])

(虽然我必须说col是变量名称的不幸选择,因为它会保留一行,而不是CSV数据列... ...