我试图检查我的函数的有效性,我注意到我的两个具有相同代码的函数给出了两个不同的输出。我需要更改其中一个的if条件,但我也需要确保它们都正常工作。可以
data_out = open("mentees_all_attributes.csv", "rU")
reader = csv.reader(data_out)
next(reader,None)
def primaryWithParticipatedCounts(jobs, count):
for line in reader:
cells = line
new_cell = cells[0], cells[6], cells[7], cells[8], cells[9], cells[
10] # name, # of participation, primary occupation/industry, secondary occupation/industry
if int(new_cell[1]) > 0: # Isolate all the participants with more than 0
primary = new_cell[2]
if primary == jobs:
count += 1
return jobs, count
print primaryWithParticipatedCounts(A012,a012counts)
def primaryWithoutParticipatedCounts(jobs, count):
for line in reader:
cells = line
new_cell = cells[0], cells[6], cells[7], cells[8], cells[9], cells[
10]
if int(new_cell[1]) > 0:
primary = new_cell[2]
if primary == jobs:
count += 1
return jobs, count
print primaryWithoutParticipatedCounts(A012,a012counts)
返回输出为:
('[A012]', 3)
('[A012]', 0)
你的两个功能似乎是一样的(除非我忽略了什么)。问题似乎出在
reader
。读取器和任何文件句柄一样,是一个iterator
,一旦您迭代了读取器中的所有行,它就被耗尽了。因此,当您对第二个函数再次使用相同的reader
时,没有更多的行可读取,并且count
最终成为0
您可以尝试以下任一操作:
lines = list(reader)
;只适用于小文件李>data_out.seek(0)
相关问题 更多 >
编程相关推荐