Reputation: 717
consider this piece of code:
csvreader = csv.reader(filter(lambda row: row[0] != '#', fcsv), delimiter= '\t')
csvrs =[[row[2], row[7]] for row in csvreader]
the element row[7]
is a string separated by ;
, what i want to do is not put the entire row[7]
inside csvrs but only a splitted part seaprated by ;
Say for example:
row[7] = '123;457;789;1011'
i want only the second position inside csvrs (in this case 457) and i want this every time filtering out every other piece. I'M trying with split with no result maintaining the list comprehension.
Upvotes: 0
Views: 50
Reputation: 4373
def get_second_col(row, delimiter=';'):
try:
return row.split(delimeter)[1]
except IndexError:
return None
csvrs =[[row[2], get_second_col(row[7])] for row in csvreader]
Upvotes: 1
Reputation: 43314
Just do the split inside the comprehension and get [1]
for the second item
csvrs =[[row[2], row[7].split(';')[1]] for row in csvreader]
Upvotes: 1