kimpster kimpster - 17 days ago 5
Python Question

Finding Column number in CSV using python

I want to match a certain string in a CSV file and return the 'column' of the string within the CSV file for example

import csv
data = [a,b,c],[d,e,f],[h,i,j]


for example I'm looking for the word 'e' i want it to return [1] as it is in the second column.

Answer

I am not sure why do you need csv in this example.

>>> data = ['a','b','c'],['d','e','f'],['h','i','j']
>>> 
>>> 
>>> string = 'e'
>>> for idx, lst in enumerate(data):
...     if string in lst:
...             print idx

1
Comments