ElizabethSchlueter
ElizabethSchlueter

Reputation: 25

How do I write a csv file from a dictionary with multiple values per key?

I originally posted this question as part of another question. I need to write a csv file in which it gives me the name of the technician, the date they worked on a tract, and the number of tracts on that date. I already found a way to get the needed data into a dictionary with the key being the name of the technician and the value being the date and the count using a cursor to look through ArcMap to find the data, using a code like so:

desc = arcpy.Describe(inLayer)
fields =  desc.fields
for field in fields:
    for srow in cursor:
        tech = srow.getValue("Technician")
        ModDate = srow.getValue("ModifiedDate")
        FormDate = ModDate.strftime("%m-%d-%Y")
        if tech == "Elizabeth":
            EScontract = EScontract + 1
            ESList = []            
            ESList.append(FormDate)
            EStech.update(ESList)
            for date in EStech.iteritems():
                if tech in Listedtech:
                    Listedtech[tech].append(date)
                else:
                    Listedtech[tech] = [date]

where EStech is a Counter dictionary that was defined earlier and Listedtech is an empty dictionary defined earier as well.

Listedtech when printed would appear to be like:

Listedtech = {u'Elizabeth': [('05-11-2015', 6), ('05-13-2015', 16), ('05-12-2015', 16)] , u'Michael': [('05-11-2015', 3)]}

How do I take my dictionary named Listedtech and turn it into a csv file?

Update

I have successfully made my dictionary into a list of dictionary called nl. When I print my list of dictionaries, I get something like this:

nl = [{u'Elizabeth': [('05-11-2015', 6), ('05-13-2015', 16), ('05-12-2015', 16)]} , {u'Michael': [('05-11-2015', 3)]}]

However, now I am having issues still getting it to a csv. When I did the DictWriter, it came out very weird were each technician had its own column but only one row with all of the associated values with it so the csv file looks something like this

Elizabeth | Michael
('05-11-2015', 6), ('05-13-2015', 16), ('05-12-2015', 16) | ('05-11-2015', 3)

instead of like how I want it in which each value is on a different row. The code I used is something like this:

with open(csvfilename, 'w') as f:
    fieldnames = ['Elizabeth', 'Michael']
    dict_writer = csv.DictWriter(f, fieldnames = fieldnames)
    dict_writer.writeheader()
    dict_writer.writerows(nl)

What am I doing wrong?

Upvotes: 1

Views: 921

Answers (1)

Open AI - Opting Out
Open AI - Opting Out

Reputation: 24153

You can use csv.DictWriter. From the docs:

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['first_name', 'last_name']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
    writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
    writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})

Upvotes: 2

Related Questions