Repeating a for in line loop python
<p>How would I repeat this (excluding the opening of the file and the setting of the variables)?
this is my code in python3    </p>

<pre><code>file = ('file.csv','r')
 count = 0 #counts number of times i was equal to 1
 i = 0 #column number 
 for line in file:
    line = line.split(",")     
    if line[i] == 1:
        count = count + 1
 i = i+1
</code></pre>

<p>The following function will return the number of fields in the csv file <code>file_name</code> whose value is <code>field_value</code>, which is what I think you are trying to do:</p>

<pre><code>import csv

def get_count(file_name, field_value):
    count = 0 
    with open(file_name) as f:
        reader = csv.reader(f)
        for row in reader:
            count += row.count(field_value)
    return count

print(get_count('file.csv', '1'))
</code></pre>

python list python-3.x csv repeat