-1

This solution sort of works but is extremely ugly:

Finding the index of an item given a list containing it in Python

rowIndex = 3
sheet = list(csv.reader(open('ObserverLog.csv'))) 
print sheet[rowIndex][sheet[0].index("Message sent? (Y/N)")] 

It seems like there ought to be a way to make it work like this:

print sheet[rowIndex]["Message sent? (Y/N)"] 
Community
  • 1
  • 1
gnarbarian
  • 2,622
  • 2
  • 19
  • 25

1 Answers1

2

DictReader will read each row as a dictionary:

list(csv.DictReader(open('ObserverLog.csv')))

Returns a list of dictionary, so

sheet[rowIndex]["Message sent? (Y/N)"] 

would work.

zw324
  • 26,764
  • 16
  • 85
  • 118
  • This works well enough. not totally what I wanted but it is exactly what I asked for. haha. An unanticipated consequence of this is I can't reference by index anymore. but that's OK. – gnarbarian May 27 '15 at 00:49
  • @gnarbarian Well if you really want to have all the power you can try using pandas, which is great :-) – zw324 May 27 '15 at 00:51