我试图检查我的函数的有效性,我注意到我的两个具有相同代码的函数给出了两个不同的输出。我需要在其中一个中更改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)
答案 0 :(得分:1)
你的两个功能似乎是相同的(除非我忽略了一些东西)。问题似乎与reader
有关。与任何文件句柄一样,阅读器是iterator
,一旦您迭代了阅读器中的所有行,它就会耗尽。因此,当您对第二个函数使用相同的 reader
时,不再需要读取行,而count
最终为{ {1}}。
您可以尝试以下任何一项:
0
;仅适用于小文件!lines = list(reader)
答案 1 :(得分:0)
data_out.seek(0) between the funcs