使用可变列数选择csv文件中的行

时间:2015-09-08 15:47:27

标签: python csv

我有一个csv文件,我需要选择某些行。对我来说很容易删除AGE和MEAN WEIGHT,因为这些名称在任何文件中都是相同的。

ID,AGE,HEIGHT,MEAN WEIGHT,20-Nov-2002,05-Mar-2003,09-Apr-2003,23-Jul-2003 1,23,1.80,80,78,78,82,82 2,25,1.60,58,56,60,60,56 3,20,1.90,100,98,102,98,102

ID,HEIGHT,20-Nov-2002,05-Mar-2003,09-Apr-2003,23-Jul-2003 1,1.80,78,78,82,82 2,1.60,56,60,60,56 3,1.90,98,102,98,102

我有这个代码

import csv
out= open("C:/Users/Pedro/data.csv") 
rdr= csv.reader(out)
result= open('C:/Users/Pedro/datanew.csv','w')
wtr= csv.writer ( result,delimiter=',',lineterminator='\n')
for row in rdr:
    wtr.writerow( (row[0], row[2], row[4],row[5],row[6],row[7]) )
out.close()
result.close()

但我的难点是选择所有有日期的列。日期的列数可以是变量。解决方案可能是检测字符 - 行[4]

1 个答案:

答案 0 :(得分:1)

我不是100%肯定你在问什么,但是这里有一个脚本可以做你想要的,这是用所有未知数量的日期列重现文件,加上你的列0和2 (ID& HEIGHT):

import csv

with open('data.csv') as infile:  # Use 'with' to close files automatically
    reader = csv.reader(infile)
    headers = reader.next()  # Read first line

    # Figure out which columns have '-' in them (assume these are dates)
    date_columns = [col for col, header in enumerate(headers) if '-' in header]

    # Add our desired other columns
    all_columns = [0, 2] + date_columns

    with open('new.csv', 'w') as outfile:

        writer = csv.writer(outfile, delimiter=',', lineterminator='\n')

        # print headers
        writer.writerow([headers[i] for i in all_columns])

        # print data
        for row in reader:  # Read remaining data from our input CSV
            writer.writerow([row[i] for i in all_columns])

这有帮助吗?