views:

199

answers:

3

I am using some python to do some variable name generation. For some reason I am only getting part of what I need.

import sys
import csv

params = csv.reader(open('params.csv'), delimiter=',', skipinitialspace=True)

flags_r = []
flags_w = []
numbers_r = []
numbers_w = []
station = ['AC1','DC1','DC1']
drive = ['','Fld','Arm']

for i in range(3):
    for p in params:
        try:
            desc = p[1].split(' ')
            desc = [part.capitalize() for part in desc]
            desc = "".join(desc)
        except IndexError, e:
            print 'IndexError: %s' %(e,)
            continue
        print station[i],drive[i],p[0]
        flags_r.append( 'mod%(station)s_%(drive)sP%(param)04dr_%(desc)s' % \
                          { 'station' : station[i], 'drive' : drive[i], 'param': int(p[0]), 'desc':desc })
        flags_w.append( 'mod%(station)s_%(drive)sP%(param)04dw_%(desc)s' % \
                          { 'station' : station[i], 'drive' : drive[i], 'param': int(p[0]), 'desc':desc })
        numbers_r.append( 'mod%(station)s_%(drive)sP%(param)04drn_%(desc)s' % \
                          { 'station' : station[i], 'drive' : drive[i], 'param': int(p[0]), 'desc':desc })
        numbers_w.append( 'mod%(station)s_%(drive)sP%(param)04dwn_%(desc)s' % \
                          { 'station' : station[i], 'drive' : drive[i], 'param': int(p[0]), 'desc':desc })

    print i

params.csv:

100, Speed Reference
101, Speed Feedback

for some reason it is outputting:

AC1 100
AC1 101
0
1
2

the reason for the try/except is to catch any blank rows or missing second fields in the csv file.

It appears that the inner loop only get executed on the first pass. The only reason I can see for this to happen would be the try/except as I have done an interactive example to test it.

+1  A: 

Make sure params is a list and not an iterator.

>>> s = (i for i in range(10))
>>> for ss in s: print(ss)

0
...
9
>>> for ss in s: print(ss)

# Nothing!
Hamish Grubijan
+6  A: 

In the first iteration of the outer loop you read all lines from params. In the second iteration all the lines from params are already read, so there is nothing left to iterate over in the inner loop.

To work around this, you could load all the data sets into a list and then iterate over that list:

reader = csv.reader(open('params.csv'), delimiter=',', skipinitialspace=True)
params = list(reader)
sth
I love python, but I am still not used to thinking it. Iterator != []
Tanj
A: 

on the first pass the reader buffer gets exhausted, so there is nothing else to read since you reached end of file.

You need to read your file in before the loops

aaa
reading the file multiple times isn't very efficient
Tanj
this is what I am meant by "read in". I will make it "loops" instead
aaa