list - Python CSV reader doesnt have any data -



list - Python CSV reader doesnt have any data -

the next code work fine.

with open(filename, 'rb') f: reader = csv.reader(f) row in reader: cur.execute(insertstatement, row)

when insert these 2 lines, goes wrong.

with open(filename, 'rb') f: reader = csv.reader(f) totalrows = len(list(reader)) print totalrows # print out right output row in reader: cur.execute(insertstatement, row)

my guess when assign totalrows = len(list(reader)) cursor moves end of file there nil happens in loop.

if true how can move cursor start without closing file , reopening it? if not please help.

that's right, file has been consumed , subsequent reads on reader not homecoming data.

you can around calling f.seek(0) on underlying file, i.e.

with open(filename, 'rb') f: reader = csv.reader(f) totalrows = len(list(reader)) print totalrows # print out right output f.seek(0) row in reader: cur.execute(insertstatement, row)

python list csv

Comments

Popular posts from this blog

model view controller - MVC Rails Planning -

ruby on rails - Devise Logout Error in RoR -

html - Submenu setup with jquery and effect 'fold' -