Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - CSV Reader - For Loop Reading The Rows, But Not Reaching The End of the File
    primarykey
    data
    text
    <p>I've developed a function to aggregate the total population of, for each given cohort, given in a file. This function currently gets used twice. Once to get the total [actual] population, and once to get the total number of 'cases'. I'm encountering a problem where the function is not reading to the end of the 'cases' file. I implemented a row counter, which prints the number of rows iterated through. The population file counter output is 933 and the case file counter output is 911, which means it is not reading the bottom 22 cases. Does anybody have any idea why this might be?</p> <p>Here is the function I have defined:</p> <pre><code>def newPopCount(filename, fileheader): rowCount = 0 # Row counter import csv popholder = [] cohorts = [] print (len(fileheader)) for i in range(3, len(fileheader)): cohorts.append(fileheader[i]) for i in range(len(cohorts)): popholder.append(0) popcsv = open(filename, 'r', newline = '') popreader = csv.reader(popcsv, delimiter = ',') for row in popreader: rowCount += 1 counter = 0 if row[0] == fileheader[0]: continue else: for i in range(3, len(fileheader)): popholder[counter] += int(row[i]) counter += 1 popcsv.close() print (rowCount) # Print row counter return popholder </code></pre> <p>By the way: <code>fileheader</code> is obtained from another function, and is just like what it sounds- the header of the file. Also, indexing begins at <code>3</code> because the first entries in the file are the zipcode, the x-coordinate and the y-coordinate.</p> <p>If anybody has any idea please share!</p> <p>This is the NEW case file, which has the data delimited, properly this time, with commas. There is also a second file, which contains example of the original state of the data. This data is aggregated in the main function call, which produces the file we are actually talking about: <a href="https://gist.github.com/FortyLashes/14760c14f653e1889e61" rel="nofollow">Cases</a></p> <p>I have also decided to include the code, which I use to get the headers. I typically call it by setting a variable equal to it: <code>thisHeader = getHeader('Cases.csv')</code> and then call the other function <code>caseRecord = newPopCount('Cases.csv', thisHeader)</code></p> <p>Here is the <code>getHeader</code> function:</p> <pre><code>`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', '!', '@', '#', '$', '%', '^', '&amp;', '*', '(', ')', '+', '+', "'", '"', '{', '}', '[', ']', '?', '.', ',', '&lt;', '&gt;', '/', '~', '`', '-', '_'] headercsv = open(file, 'r', newline = '') headerreader = csv.reader(headercsv, delimiter = ',') for row in headerreader: if row[0][0] in charList and row[1][0] in charList: headername = row headercsv.close() return headername` </code></pre> <p>Again, thanks for taking a look!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload