简体   繁体   English

将字典列表写入CSV Python

[英]Write list of dictionary into CSV Python

Suppose I have the list of dictionary dataset like this, 假设我有这样的字典数据集列表,

data_set = [
    {'Active rate': [0.98, 0.97, 0.96]},
    {'Operating Expense': [3.104, 3.102, 3.101]}
]

I need to iterate the list of dictionary and put the keys as column headers and its values as the rows and write it to the CSV file. 我需要迭代字典列表并将键作为列标题及其值作为行并将其写入CSV文件。

Active rate    Operating Expense
0.98           3.104
0.97           3.102
0.96           3.101

This is what I tried 这是我试过的

data_set = [
    {'Active rate': [0.98, 0.931588, 0.941192]},
    {'Operating Expense': [3.104, 2.352, 2.304]}
]

import csv

with open('names.csv', 'w') as csvfile:
    fieldnames = ['Active rate', 'Operating Expense']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    writer.writerow({'Active rate': 0.98, 'Operating Expense': 3.102})
    writer.writerow({'Active rate': 0.97, 'Operating Expense': 3.11})
    writer.writerow({'Active rate': 0.96, 'Operating Expense': 3.109})

For brevity, I have reduced the keys to 2 and list of values to 3. 为简洁起见,我将键减少到2,将值列表减少到3。

How to approach this problem? 如何解决这个问题?

Thanks 谢谢

The following approach should work for the data structure you have given: 以下方法适用于您提供的数据结构:

import csv

data_set = [
    {'Active rate': [0.98, 0.97, 0.96]},
    {'Operating Expense': [3.104, 3.102, 3.101]}
]

fieldnames = ['Active rate', 'Operating Expense']
rows = []

for field in fieldnames:
    for data in data_set:
        try:
            rows.append(data[field])
            break
        except KeyError, e:
            pass

with open('names.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)
    csv_output.writerow(fieldnames)
    csv_output.writerows(zip(*rows))

Giving you the following CSV output file: 为您提供以下CSV输出文件:

Active rate,Operating Expense
0.98,3.104
0.97,3.102
0.96,3.101
d1 = {'Active rate': [0.98, 0.931588, 0.941192]}
d2 = {'Operating Expense': [3.104, 2.352, 2.304]}

with open('names.csv', 'w') as csvfile:
    fieldnames = zip(d1, d2)[0]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()

    for row in zip(d1['Active rate'], d2['Operating Expense']):
        writer.writerow(dict(zip(fieldnames, row)))

For performance, you might want to use itertools.izip over zip depending on the length of lists. 为了提高性能,您可能希望在zip上使用itertools.izip ,具体取决于列表的长度。

(This answer has the disadvantage of using an external library, but) (这个答案有使用外部库的缺点,但是)

pandas already provides extraordinarily powerful and simple tools for dealing with csv files. pandas已经提供了非常强大而简单的工具来处理csv文件。 You can use to_csv . 你可以使用to_csv

Note your data structure is structured awkwardly, so we first transform it to a more intuitive structure 请注意,您的数据结构结构笨拙,因此我们首先将其转换为更直观的结构

data_set2 = { x.keys()[0] : x.values()[0] for x in data_set }

import pandas as pd
df = pd.DataFrame(data_set2)
df.to_csv('names.csv', index = False)
 data_set = [ {'Active rate': [0.98, 0.97, 0.96]}, {'Operating Expense': [3.104, 3.102, 3.101]} ] 

Firstly, just a quick comment, your initial data structure doesn't necessarily make sense as it is. 首先,只是一个快速评论,您的初始数据结构不一定有意义。 You're using a list of dicts, but each dict seems to be using only one key, which seems to defeat its purpose. 你正在使用一个dicts列表,但每个dict似乎只使用一个键,这似乎打败了它的目的。

Other data structures that would make more sense would be something like this (where each dict structure is used, as you currently have it, for one label/value pair, but at least the dict is used to tell the label and the value): 其他更有意义的数据结构将是这样的(使用每个dict结构,就像你现在拥有的那样,对于一个标签/值对,但至少dict用于告诉标签和值):

data_set = [
    {'label': 'Active rate', 'values': [0.98, 0.97, 0.96]},
    {'label': 'Operating Expense', 'values': [3.104, 3.102, 3.101]}
]

or, possibly better, an OrderedDict that give you both the order of your initial data set and the key/value mapping benefits: 或者,更好的是, OrderedDict为您提供初始数据集的顺序和键/值映射的好处:

from collections import OrderedDict
data_set = OrderedDict()
data_set['Active rate'] = [0.98, 0.97, 0.96]
data_set['Operating Expense'] = [3.104, 3.102, 3.101]

Of course, we don't always choose the data structures we get, so let's assume you can't change it. 当然,我们并不总是选择我们得到的数据结构,所以我们假设你不能改变它。 Your question then becomes a problem of swapping the roles of rows and columns from your initial dataset. 然后,您的问题就成了从初始数据集交换行和列角色的问题。 Effectively, you want to iterate through multiple lists at the same time, and for this, zip is very useful. 实际上,您希望同时迭代多个列表,为此, zip非常有用。

import csv

fieldnames = []
val_lists = []
for d in data_set:
    # Find the only used key.
    # This is a bit awkward because of the initial data structure.
    k = d.keys()[0]
    fieldnames.append(k)
    val_lists.append(d[k])

with open('names.csv', 'w') as csvfile:
    writer = csv.writer(csvfile)    
    writer.writerow(fieldnames)

    for row in zip(*val_lists):
        # This picks one item from each list and builds a list.
        # The first row will be [0.98, 3.104]
        # The second row will be [0.97, 3.102]
        # ...
        writer.writerow(row)

Note that there is no need for a DictWriter when you're using zip , since that would mean you need to rebuild a dict without any real benefit. 请注意,当您使用zip ,不需要DictWriter ,因为这意味着您需要重建dict而没有任何实际好处。

This code will help you without being tied to a certain number of dicts inside data_set 此代码将帮助您,而不必依赖于data_set一定数量的data_set

I've added another dict with 'Losses' key, to test 我添加了另一个带有'损失'键的词典来测试

import csv

data_set = [
    {'Active rate': [0.98, 0.97, 0.96]},
    {'Operating Expense': [3.104, 3.102, 3.101]},
    {'Losses': [1.14, 2.28, 3.42]}
]

headers = [d.keys()[0] for d in data_set]

with open('names.csv', 'w') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=headers)
    writer.writeheader()
    for item in zip(*[x.values()[0] for x in data_set]):
        more_results = list()
        more_results.append(headers)
        more_results.append(item)
        writer.writerow(dict(zip(*more_results)))

Output: 输出:

在此输入图像描述

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

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