purcell / airspeed

A lightweight Python template engine compatible with Velocity, used in OpenStack
Other
91 stars 37 forks source link

Csv Reader not treated as Iterable? #27

Closed saviourofdp closed 7 years ago

saviourofdp commented 7 years ago

I'm not an experienced Python developer but if I do the following (Python 2.7)

with open(csvfile, 'rb') as inputfile:
        reader = csv.DictReader(inputfile) # or csv.reader(inputfile)
        variables['reader'] = reader

print template.merge(variables, loader = loader)

with this template

#foreach($row in $reader)
## use $row here
#end

I get an error

ValueError: value for $row is not iterable in #foreach: <csv.DictReader instance at 0x02C38030>

but I have to change

variables['reader'] = reader

to

variables['reader'] = list(reader)

to get it to work. Is that expected behaviour? It was my impression that the csv reader could be treated as iterable, seeing as in standard python this works:

for row in reader
    print row