1

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?

1 Answers1

2

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'})
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
  • I have ready the docs but I am unsure how to make it work for this? how do you use this and make it look at a dictionary that is already formed? – ElizabethSchlueter May 13 '15 at 21:20
  • Ah, I see. You need them separate. – Peter Wood May 13 '15 at 21:54
  • Is there a way to do this? This would work normally but I don't know how to make it work for this. – ElizabethSchlueter May 14 '15 at 16:10
  • You need to convert you dictionary of lists into a list of dictionaries. I don't have time to answer now, maybe in a few hours. – Peter Wood May 14 '15 at 16:19
  • See [this question](http://stackoverflow.com/questions/5558418/list-of-dicts-to-from-dict-of-lists) for how. – Peter Wood May 14 '15 at 16:21
  • I am still new to this, but what is the difference between a dictionary or lists and a list of dictionaries and how does it help? – ElizabethSchlueter May 14 '15 at 18:52
  • The example above shows you have to write a dictionary for each line in the csv. So you need a list of dictionaries to loop over. At the moment you have a single dictionary with values which are lists (all of the same length). The linked question should show you how to get from one to the the other (and back again). – Peter Wood May 14 '15 at 18:53
  • Thank you again Peter Wood for helping me learn all of this :) – ElizabethSchlueter May 15 '15 at 16:29
  • I read the question that you sent me and found a way to create a list of dictionaries. Now can I just print like normal? – ElizabethSchlueter May 15 '15 at 17:50