kimpster kimpster - 1 year ago 90
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download