Monday 15 August 2011

python - Returning a row from a CSV, if specified value within the row matches condition -


Ah, I am writing a Python script to filter some large CSV files.

I only want to keep the lines that meet my criteria.

My input is a CSV file in the following format

  locus total dipth average_depth_sample Depth_for_17 chr1: 6484996 1030 1030 1030 chr1: 6484997 14 14 14 chr1: 6484998 0    

I want to return the lines where the total diapith is 0.

I want to read statistics. But I am trying to parse the rows and draw a line to fulfill my situation.

Even I have the code:

  import csv f = open ("file path", 'rb') reader = csv.reader (f) ) #reader object that resets to a csv file (f) header = reader.next () # header variable column = {} # List of columns for columns in header: # Header column for row in each reader [H ] = []: For every line in the #Reader object, V (header, line) in the zip: Row value (V) in a series of Tupless column [H] with the name # combin header. Pend (V) # Add each call to the respective column   

I understand that my data is now in a dictionary format, and I want to filter it by "Total_Depth" key , But I am unsure how to do this. I aim to use the 'if' statement to select relevant rows, but it does not understand how it is done with dictionary structure.

Any advice would be greatly appreciated by SB :)

Understand the list Use the. Open CSV with import ("Filepath", 'rb') as f: reader = csv.DictReader (f) rows = [row ['Total_Depth' for line in line]

  ]! = '0'] for lines in rows: print line   



No comments:

Post a Comment