5

At the moment, I have a list directly in my code and am using enumerate so I can use the list index numbers for the user to select one of the items. Looks like this (just header row included)

fmpList = [['Date', 'ID', 'Plot No', 'Modified', 'Lat', 'Long']......
for item in enumerate(fmpList[]):            
    print "[%d] %s" % item

This works well but I don't want to put the list in the function but rather to read the csv from file. The alternative I used to do this is...

import csv
with open ('fmpList.csv', 'rU') as csvfile: 
        next (csvfile, None)
        plotlist = csv.reader(csvfile, delimiter=',', dialect=csv.excel_tab)
        for row in enumerate (plotlist):
            print '[%s]' %(item[2])

This also works but I can't seem to combine the 2... read the csv file in, then enumerate to be able to have a selection based on index. Help would be greatly appreciated

EDIT: So now I've got working code as I wanted...

import csv
with open ('fmpList.csv', 'rU') as csvfile: 
        next (csvfile, None)
        plotlist = csv.reader(csvfile, delimiter=',', dialect=csv.excel_tab)
        for row in enumerate (plotlist):
            print '[%s]' %(item[2])

But the last line is not giving me column 2 only displayed. In fact, I'm after column 0 (the index no) and 2 (the plot name). [0] gives the first column...cool, but any number greater than 1 gives IndexError: ..out of range. Tried every combo but nothing works

Op.Ivy
  • 81
  • 1
  • 3
  • 9
  • 4
    `for i,row in enumerate(plotlist)`? – sapi Apr 22 '13 at 04:43
  • See http://stackoverflow.com/questions/6410982/enumerate-items-in-a-list-so-a-user-can-select-the-numeric-value – gimel Apr 22 '13 at 04:54
  • How is your CSV formatted. Delimiter may only be one char and right now you have tab AND space – jamylak Apr 22 '13 at 04:54
  • the csv data is just separated by commas. I actually just added the tab delimiter, but didnt remove the space – Op.Ivy Apr 22 '13 at 05:00
  • thanks sapi- I had that line when I combined the 2, but needed to leave out the for row in plotlist and just have.. for item in enumerate (fmpList[]): Looks like it's working now. stupid me – Op.Ivy Apr 22 '13 at 05:05
  • In your later addition, where are you defining item? Don't you want %(row[2]))? – mfitzp Apr 22 '13 at 10:06

1 Answers1

9

enumerate(...) works on any iterable

import csv
with open ('fmpList.csv', 'rU') as csvfile: 
    next(csvfile, None) # skip header
    plotlist = csv.reader(csvfile, delimiter=',', dialect=csv.excel_tab)
    for i, row in enumerate(plotlist):
        print "[%d] %s" % (i, row)
jamylak
  • 128,818
  • 30
  • 231
  • 230
  • That works well, but I also had for row in enumerate(plotlist[1:]): to get rid of the header line but now I get a TypeError. How do I do this? – Op.Ivy Apr 22 '13 at 05:27
  • @Op.Ivy You can't slice files with the list slicing syntax, I've just added a call to `next`, to advance the file iterator by one item to skip the header – jamylak Apr 22 '13 at 05:28
  • Brilliant, thank you. And if I can push for one more solution.. What if I just want to display only column 2 instead of all 7? – Op.Ivy Apr 22 '13 at 05:32
  • @Op.Ivy I'm assuming column 2 is the `2nd` column, so 0-indexed you would just change `row`, to `row[3]` – jamylak Apr 22 '13 at 05:33
  • That's what I thought but I get TypeError: %d format: a number is required, not list – Op.Ivy Apr 22 '13 at 05:40
  • @Op.Ivy Did you change the `'%s'` to `'%d'`? In that case you would need an `int` and you would have to do `int(row[3])` but if you leave it as `'%s'` then it's fine. – jamylak Apr 22 '13 at 05:44
  • @jamylak.. I tried '%s' only but if I put anything higher than 1 in row[1], I get 'tuple index out of range', but there should be 5 ikn range. I actually need column 0 and 2...any ideas? – Op.Ivy Apr 22 '13 at 08:44
  • @Op.Ivy Please add your updated code to the question, It's hard to imagine what you are doing – jamylak Apr 22 '13 at 08:47
  • @Op.Ivy you forgot the `enumerate` gives you index-item pairs. Your line should be `for i, item in enumerate (plotlist): print '[%s]' %(item[2])` – jamylak Apr 22 '13 at 09:26
  • A bit more robust: `csvin = csv.reader(...)`, then use `for row in islice(csvin, 1, None)` - this has the advantage that an arbitrary number of lines can be skipped and also, should rows have quoted columns containing `\n` the correct number of "lines" will be skipped... – Jon Clements Apr 22 '13 at 09:29
  • @jamylak..thanks, it works but can't display a 2nd column as well(col 0, which has the index number). Would've though it'd be (item[0,2]). Doesn't matter too much..should just display it all. cheers – Op.Ivy Apr 22 '13 at 09:49
  • @Op.Ivy `"%s %s"%(item[0], item[2])` – jamylak Apr 22 '13 at 09:59