Reading from CSV and filtering columns
<p>I have a CSV file.</p>

<p>There are a fixed number of columns and an unknown number of rows. </p>

<p>The information I need is always in the same 2 columns but not in the same row.</p>

<p>When column 6 has a 17 character value I also need to get the data from column 0. </p>

<p>This is an example row from the CSV file:</p>

<pre><code>E4:DD:EF:1C:00:4F, 2012-10-08 11:29:04, 2012-10-08 11:29:56, -75, 9, 18:35:2C:18:16:ED,
</code></pre>

<p>You can use <strong>csv</strong> module to read the csv files and you can  provide delimiter/dialect as you need (, or | or tab etc..) while reading the file using csv reader. 
csv reader takes care of providing the row/record with columns as list of values. If you want access the csv record/row as dict then you can use <strong>DictReader</strong> and its methods.</p>

<pre><code>import csv
res = []
with open('simple.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        # Index start with 0 so the 6th column will be 5th index
        # Using strip method would trim the empty spaces of column value
        # Check the length of columns is more than 5 to handle uneven columns
        if len(row) &gt; 5 and len(row[5].strip()) == 17:
            res.append(row[0])

# Result with column 0 where column 6 has 17 char length
print res
</code></pre>

python csv