Retrieve header of column if it has a certain value
<p>I have a csv file with headers in the following format:</p>

<p><code>column1</code>,<code>column2</code>,<code>column3</code></p>

<p><code>True</code>,<code>False</code>,<code>False</code></p>

<p><code>False</code>,<code>True</code>,<code>True</code></p>

<p>In python, I would like to print the column name if a value of <code>True</code> is under it (<code>column1</code> and then for the next row <code>column2</code> and <code>column3</code>). In the code I have below, it's printing every column.</p>

<pre><code>    with open(reportOut, 'r') as f:
        reader = csv.reader(f, skipinitialspace=True)
        header = next(reader)
        for row in reader:
            if 'True' in row:
                print(header)
</code></pre>

<p>This works:</p>

<pre><code>import csv

with open("my.csv", 'r') as f:
    reader = csv.reader(f, skipinitialspace=True)
    headers = next(reader)
    # Start counting from 2 (Row #1 is headers)
    for row_number, row in enumerate(reader, 2):
        for column, val in enumerate(row):  # On each column in the row
            if val == "True":  # Check for the value
                # Print the header according to the column number
                print(row_number, headers[column])
</code></pre>

<p>Output:</p>

<pre><code>2 column1
3 column2
3 column3
</code></pre>

python python-3.x csv