I’m new to Python and am trying to make use of it to parse and manipulate data from a CSV file. In my script below there are portions of code that are not executing. However it seems to me that they should unless I am misunderstanding something pretty fundamental about Python syntax. Please see the comments in the code. I indicated using the comments what code is not firing. Can someone tell me what the problem might be?
import csv
import datetime
def uniqueify(file):
checked = []
reader = csv.reader(file)
for row in reader:
if row[1] not in checked:
checked.append(row[1])
return checked
f = open('d:/test.csv')
reader = csv.reader(f)
print "Parsed file is:", f.name
offers = uniqueify(f)
print "Offers", "|",
print "Channel", "|",
start = datetime.date(2011, 12, 01)
end = datetime.date(2012, 01, 31)
d = start
while (d<=end):
print d, "|",
d += datetime.timedelta(days=1)
print #force to new line
for o in offers:
print o,"|", "DRTV","|"
for row in reader:
#NOTHING INSIDE THIS FOR LOOP EXECUTES
print row
if row[1] == o:
print "foo"
date = datetime.date(row[0])
padding = date - start
print padding
#NOTHNG BELOW THIS LINE EXECUTES
for row in reader:
print row
print "foo"
Try adding a
after you call
uniqueify(f). I think you’re exhausting the file object that is backing the CSV reader.