Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy keys to a new dictionary (Python)

Tags:

I'm reading a csv file, using DictReader(). The function returns a dictionary, where the header items are the keys and the cells are the values. Pretty cool.

But I'm trying to account for rows where the data may not be what I expect it to be. In that case (I'm catching a ValueError exception), I would like the rows that are 'suspect' to go into a separate dictionary, for manual processing.

My question is this: since my first dictionary (the object returned by DictReader) has all of its keys set up properly, how do I copy just the keys into my second dictionary, the one which I want to be just a dictionary of suspect rows, to be manually processed?

I've been toying around with dict.fromkeys() and such for a while now and I'm just not getting anywhere. Halp!

EDIT: Pasting some of my erroneous code. Going to go hide in shame of my code. Don't judge me! ;-)

unsure_rows = dict.fromkeys(dict(csv_reader).keys(), []) for row in csv_reader: #   if row['Start Time'] != 'None':     try:         if before_date > strptime(row['Start Time'], '%Y-%m-%d %H:%M:%S') > after_date:             continue     except ValueError:         unsure_rows += row 

ValueError: dictionary update sequence element #0 has length 13; 2 is required

like image 716
Harv Avatar asked Sep 10 '11 02:09

Harv


People also ask

How can you copy the entire dictionary to a new dictionary?

The dict. copy() method returns a shallow copy of the dictionary. The dictionary can also be copied using the = operator, which points to the same object as the original. So if any change is made in the copied dictionary will also reflect in the original dictionary.

How do you duplicate a dictionary key?

Use a list in a dictionary to associate more than one value with a key. Create a dictionary with lists as values. Use a list to contain more than one value. Though the key isn't duplicated, multiple values are now assigned to the same key.


1 Answers

You are close. Try:

dict.fromkeys(my_csv_dict.keys(),[])

This will initialize a dictionary with the same keys that you parsed from your CSV file, and each one will map to an empty list (to which, I assume, you will append your suspect row values).


Try this. (There are several subtler changes here that are all necessary, like how you can't initialize unsure_rows before you start reading the CSV.)

unsure_rows = None for row in csv_reader: #   if row['Start Time'] != 'None':     try:         if before_date > strptime(row['Start Time'], '%Y-%m-%d %H:%M:%S') > after_date:             continue     except ValueError:         if not unsure_rows:             # Initialize the unsure rows dictionary             unsure_rows = dict.fromkeys(csv_reader.fieldnames,[])         for key in unsure_rows:             unsure_rows[key].append(row[key]) 
like image 136
cheeken Avatar answered Sep 27 '22 19:09

cheeken