Note that there are some explanatory texts on larger screens.

plurals
  1. POReturning a row from a CSV, if specified value within the row matches condition
    primarykey
    data
    text
    <p>Ahoy, I'm writing a Python script to filter some large CSV files.</p> <p>I only want to keep rows which meet my criteria.</p> <p>My input is a CSV file in the following format</p> <blockquote> <pre><code>Locus Total_Depth Average_Depth_sample Depth_for_17 chr1:6484996 1030 1030 1030 chr1:6484997 14 14 14 chr1:6484998 0 0 0 </code></pre> </blockquote> <p>I want to return lines where the Total_Depth is 0.</p> <p>I've been following <a href="https://stackoverflow.com/questions/3428532/how-to-import-a-csv-file-using-python-with-headers-intact-where-first-column-is">this answer</a> to read the data. But am stuck trying to parse over the rows and pull out the lines that meet my condition.</p> <p>Here is the code I have so far:</p> <pre><code>import csv f = open("file path", 'rb') reader = csv.reader(f) #reader object which iterates over a csv file(f) headers = reader.next() #assign the first row to the headers variable column = {} #list of columns for h in headers: #for each header column[h] = [] for row in reader: #for each row in the reader object for h, v in zip(headers, row): #combine header names with row values (v) in a series of tuples column[h].append(v) #append each value to the relevant column </code></pre> <p>I understand that my data is now in a dictionary format, and I want to filter it based on the "Total_Depth" key, but I am unsure how to do this. I'm aiming to use an 'if' statement to select the relevant rows, but not sure how to do this with the dictionary structure.</p> <p>Any advice would be greatly appreciated. SB :)</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.
 

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