Iteratively copy specific rows from CSV file to new file
python
Solution
You're almost there! The code is nearly correct :)
Accessing dicts goes like this:
some_dict['some_key']
Instead of:
some_object.some_attribute
Creating a string isn't done with `str(...)` but with quotes like `CG11710`
In your case:
for row in reader:
if row['gene_id'] == 'CG11710':
writer.writerow(row)
Problem
I have a large tab-delimited csv file with the following format: ``` #mirbase_acc mirna_name gene_id gene_symbol transcript_id ext_transcript_id mirna_alignment gene_alignment mirna_start mirna_end gene_start gene_end genome_coordinates conservation align_score seed_cat energy mirsvr_score ``` What I would like to be able to do is iterate through rows and select items based on data (strings) in the "gene_id" field, then copy those rows to a new file. I am a python noob, and thought it would be a good way to get my feet wet, but it is harder than it looks! I have been trying to use the csv package to manipulate the files, reading and writing basic stuff using dictreader and dictwriter. If anyone can help me out coming up with a template for the iterative searching aspect, I would be greatly indebted. So far I have: ``` import csv f = open("C:\Documents and Settings\Administrator\Desktop\miRNA Scripting\mirna_predictions_short.txt", "r") reader = csv.DictReader(f, delimiter='\t') writer = open("output.txt",'wb') writer = csv.writer(writer, delimiter='\t') ``` Then the iterative bit, bleurgh: ``` for row in reader: if reader.gene_id == str(CG11710): writer.writerow ``` This obviously doesnt work. Any ideas on better ways to structure this??