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
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.
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.
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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With