kimpster kimpster - 5 months ago 50
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.


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