Python - CSV Reader - For Loop Reading The Rows, But Not Reaching The End of the File -
i've developed function aggregate total population of, each given cohort, given in file. function gets used twice. once total [actual] population, , once total number of 'cases'. i'm encountering problem function not reading end of 'cases' file. implemented row counter, prints number of rows iterated through. population file counter output 933 , case file counter output 911, means not reading bottom 22 cases. have idea why might be?
here function have defined:
def newpopcount(filename, fileheader): rowcount = 0 # row counter import csv popholder = [] cohorts = [] print (len(fileheader)) in range(3, len(fileheader)): cohorts.append(fileheader[i]) in range(len(cohorts)): popholder.append(0) popcsv = open(filename, 'r', newline = '') popreader = csv.reader(popcsv, delimiter = ',') row in popreader: rowcount += 1 counter = 0 if row[0] == fileheader[0]: continue else: in range(3, len(fileheader)): popholder[counter] += int(row[i]) counter += 1 popcsv.close() print (rowcount) # print row counter return popholder
by way: fileheader
obtained function, , sounds- header of file. also, indexing begins @ 3
because first entries in file zipcode, x-coordinate , y-coordinate.
if has idea please share!
this new case file, has data delimited, time, commas. there second file, contains example of original state of data. data aggregated in main function call, produces file talking about: cases
i have decided include code, use headers. typically call setting variable equal it: thisheader = getheader('cases.csv')
, call other function caserecord = newpopcount('cases.csv', thisheader)
here getheader
function:
`def getheader(file): import csv headername = none charlist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '+', "'", '"', '{', '}', '[', ']', '?', '.', ',', '<', '>', '/', '~', '`', '-', '_'] headercsv = open(file, 'r', newline = '') headerreader = csv.reader(headercsv, delimiter = ',') row in headerreader: if row[0][0] in charlist , row[1][0] in charlist: headername = row headercsv.close() return headername`
again, taking look!
i downloaded gist , saved cases.tsv
.
then modified newpopcount
popcsv.readline()
right after open
ing file, , changed next line use delimiter='\t'
instead of delimiter=','
.
i ran line:
h = newpopcount('cases.tsv', ['zcta', 'xcoord', 'ycoord', 'm5064', 'm6574', 'm75plus', 'f5064', 'f6574', 'f75plus'])
it printed out 932.
since there 933 lines, 1 of header (and not counted), that's right answer.
so, best guess ran on wrong file, , that's why got wrong answer.
it's not impossible there's bug in code, , incorrect sample data uploaded happens counter bug… seems unlikely. if can give actual file, , code runs on file, , code calls newpopcount
function, should trivial rule possibility out.
Comments
Post a Comment