Question

I have a big list that I pulled in from a .csv:

CSV_PATH = 'myfile.csv'
CSV_OBJ = csv.DictReader(open(CSV_PATH, 'r'))
CSV_LIST = list(CSV_OBJ)

And I only want to keep some of the columns in it:

KEEP_COLS = ['Name', 'Year', 'Total Allocations', 'Enrollment']'

It seems from Removing multiple keys from a dictionary safely like this ought to work:

BETTER = {k: v for k, v in CSV_LIST if k not in KEEP_COLS}

But I get an error: ValueError: too many values to unpack What am I missing here? I could write a loop that runs through CSV_LIST and produces BETTER by keeping only what I want, but I suspect that using comprehension is more pythonic.

As requested, a chunk of CSV_LIST

{'EIN': '77-0000091',
  'FR': '28.4',
  'Name': 'Org A',
  'Enrollment': '506',
  'Total Allocations': '$34214',
  'geo_latitude': '37.9381775755',
  'geo_longitude': '-122.3146910612',
  'Year': '2009'},
 {'EIN': '77-0000091',
  'FR': '28.4',
  'Name': 'Org A',
  'Enrollment': '506',
  'Total Allocations': '$34214',
  'geo_latitude': '37.9381775755',
  'geo_longitude': '-122.3146910612',
  'Year': '2010'}

At the commandline I can do csvcut -c 'Name','Year','Total Allocations','Enrollment' myfile.csv > better_myfile.csv but that's definitely not pythonic.

Était-ce utile?

La solution

Your dictionary comprehension is fine, but since you have a list of dictionaries, you have to create a list comprehension using that dictionary comprehension for the individual list items. Also, since you want to keep those columns, I guess you should drop that not. Try this:

[{k: v for k, v in d.items() if k in KEEP_COLS} for d in CSV_LIST]

Autres conseils

An alternative is to use

CSV_LIST = map(operator.itemgetter(*KEEP_LIST), CSV_OBJ)

This will create a list of tuples with the desired columns.

The issue is that CSV_LIST is a list, not a single dict. @tobias explained how to unpack it correctly.


However, if you're worried about being Pythonic, why are you processing a DictReader into a list of dictionaries and then filtering out all but a few keys? Without knowing your use case I can't be sure, but it's likely that it would be cleaner and simpler to just use the DictReader row-by-row the way it was intended to be used:

with open(CSV_PATH, 'r') as f:
    for row in csv.DictReader(f):
        process(row['Name'],row['Year'],row['Total Allocations'],row['Enrollment'])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top