I've been using the following code to call columns based on their headers.
def GetValuesFromColumn(title):
values = []
rownum = 0
with open(file, 'r') as f:
reader = csv.reader(f)
for row in reader:
if rownum == 0:
index = row.index(title)
rownum = 1
else:
values.append(row[index])
return values
It's working fine. But I'm currently working on such files where there could be more than one row with same header and my script gives just the first column. Instead, I'd like to call the column by checking if it has a particular word. For instance, consider there are three columns with the name 'data'. The first data column has info about tissue, second about cell, third about organism like below
data,data,data
ab tissue, cell: b cells, organism: human
bc gf tissue, cell: d cells, organism: human
bc gf tissue, cell: e cells, organism: human
then I'd like to be able to call 'tissue' and get data from first data column in this format - ab,bc gf. How can I do that?
split(','). Then checking if the word 'tissue' is there in the string at index 0, and if it is, print the string after splitting it again, based on spaces. Sayvalue = 'ab tissue'then you can get the name asvalue.split(' ')[0]for w in value.split(' '): if 'tissue' not in w: print w