now, i'm reading specified columns, want read specified row
code :
colums_001=['a','d'] f = open("data.csv") reader = csv.reader(f) headers = none row in reader: if not headers: headers = [] j, col in enumerate(row): if col in columns_001: headers.append(j) else: result_001.append(tuple([float(row[j]) j in headers])) my csv file :
dt b c d 15000 13 24 12 14 15004 14 15 25 35 15008 25 24 23 68 15012 14 12 58 98 15016 52 45 24 13 i want read , save this:
dt d 15008 25 68 15012 14 98 what should do?
import csv columns_001=['a','d'] f = open("data.csv") reader = csv.reader(f) # headers , find indices want headers = [i in reader.next()[0].split(' ') if != ''] indices = [i in range(len(headers)) if headers[i] in columns_001] wanted_times = ['15008', '15012'] result_001 = [] row in reader: row = [i in row[0].split(' ') if != ''] # columns of interest , if wanted values of dt cols_of_interest = [row[j] j in indices if row[0] in wanted_times] if cols_of_interest: # add dt cols = [row[0]] cols.extend(cols_of_interest) result_001.append(cols) new_header = ['dt'] new_header.extend(columns_001) then can put in file or something
print new_header print result_001 ['dt', 'a', 'd'] [['15008', '25', '68'], ['15012', '14', '98']]
Comments
Post a Comment