简体   繁体   English

如何从每个键具有多个值的字典中写入一个csv文件?

[英]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. 我需要编写一个csv文件,其中提供了技术人员的姓名,他们在一个区域上工作的日期以及该日期的区域数。 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: 我已经找到了一种将所需数据放入字典的方法,其中的键是技术人员的姓名,值是日期和计数,它使用游标浏览ArcMap来查找数据,方法如下:

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. 其中EStech是较早定义的Counter字典,而Listedtech也是较早定义的空字典。

Listedtech when printed would appear to be like: Listedtech印刷时看起来像:

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? 如何将名为Listedtech的词典转换为csv文件?

Update 更新

I have successfully made my dictionary into a list of dictionary called nl. 我已经成功地将我的字典制作成名为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. 但是,现在我仍然遇到问题,无法将其发送到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 当我做DictWriter时,每个技术人员都有自己的列但只有一行包含所有关联的值,结果非常奇怪,因此csv文件看起来像这样

Elizabeth | 伊丽莎白| Michael 迈克尔
('05-11-2015', 6), ('05-13-2015', 16), ('05-12-2015', 16) | ('05 -11-2015',6),('05 -13-2015',16),('05 -12-2015',16)| ('05-11-2015', 3) ('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? 我究竟做错了什么?

You can use csv.DictWriter . 您可以使用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'}) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM