2

I had a tsv file like such

Name    School   Course         
Nicole  UVA      Biology
Jenna   GWU      CS

from there, I only want to print the Name and the Course from that dictionary. How would I go about this?

The code below is how I put the original TSV file into the dictionary above.

import csv
data = csv.reader(open('data.tsv'),delimiter='\t')
fields = data.next()
for row in data:
    item = dict(zip(fields, row))
    print item 

So now I got a dictionary like such:

{'Name':'Nicole.', 'School':'UVA.','Course':'Biology'}
{'Name':'Jenna.', 'School':'GWU','Course':'CS'}
{'Name':'Shan', 'School':'Columbia','Course':'Astronomy'}
{'Name':'BILL', 'School':'UMD.','Course':'Algebra'}

I only want to print the Name and the Course from that dictionary. How would I go about this?

I want to add code so that I'm only printing

{'Name':'Jenna.','Course':'CS'}
{'Name':'Shan','Course':'Astronomy'}
{'Name':'BILL','Course':'Algebra'}

Please guide. Thank You

Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
NR567
  • 13
  • 3

6 Answers6

0

Just delete the key in the loop

for row in data:
    item = dict(zip(fields, row))
    del item['School']
    print item 
Navidad20
  • 832
  • 7
  • 11
  • for this one, can you had other variables in the del item? like can i do del item['School','Name'] because I tried that and it's not letting me. – NR567 Jan 25 '17 at 15:51
  • you would do: `del item['School']` `del item['Name']` on separate lines – Navidad20 Jan 25 '17 at 16:39
0

The easiest way is to just remove the item['School'] entry before printing

for row in data:
    item = dict(zip(fields, row))
    del item['School']
    print item 

But this only works if you know exactly what the dictionary looks like, it has no other entries that you don't want, and it already has a School entry. I would instead recommend that you build a new dictionary out of the old one, only keeping the Name and Course entries

for row in data:
    item = dict(zip(fields, row))
    item = {k, v for k, v in item.items() if k in ('Name', 'Course')}
    print item 
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
0

Maybe use a DictReader in the first place, and rebuild the row-dict only if key matches a pre-defined list:

import csv

keep = {"Name","Course"}
data = csv.DictReader(open('data.tsv'),delimiter='\t')

for row in data:
    row = {k:v for k,v in row.items() if k in keep}
    print(row)

result:

{'Course': 'Biology', 'Name': 'Nicole'}
{'Course': 'CS', 'Name': 'Jenna'}
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
0

Based on the answer here: filter items in a python dictionary where keys contain a specific string

print {k:v for k,v in item.iteritems() if "Name" in k or "Course" in k}
Community
  • 1
  • 1
DerFaizio
  • 148
  • 7
0

You're better off using a library designed for these kinds of tasks (Pandas). A dictionary is great for storing key-value pairs, but it looks like you have spreadsheet-like tabular data, so you should choose a storage type that better reflects the data at hand. You could simply do the following:

import pandas as pd

df = pd.read_csv('myFile.csv', sep = '\t')

print df[['Name','Course']]

You'll find that as you start doing more complicated tasks, it's better to use well written libraries than to cludge something together

flyingmeatball
  • 7,457
  • 7
  • 44
  • 62
0

replace the your " print(item) " line with the below line.

print(dict(filter(lambda e: e[0]!='School', item)))

OUTPUT:

{'Name':'Jenna.','Course':'CS'}
{'Name':'Shan','Course':'Astronomy'}
{'Name':'BILL','Course':'Algebra'}