我是Python的新手,我正在尝试利用它来解析和操纵CSV文件中的数据。在我下面的脚本中,有部分代码没有执行。然而,在我看来,除非我误解了Python语法的一些基本内容,否则它们应该存在。请参阅代码中的注释。我指出使用注释什么代码没有触发。有人能告诉我问题可能是什么吗?
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"
答案 0 :(得分:4)
尝试添加
f.seek(0)
致电uniqueify(f)
后。我认为你正在耗尽支持CSV阅读器的文件对象。