水平写入CSV [英] Writing a CSV horizontally
问题描述
假设我们正在从一些来源中读取具有多个键值对的数据。让我们使用下面的列表作为例子:
[{'key0':'key0_value0','key1':'key1_value0' },
{'key0':'key0_value1','key1':'key1_value1'}]
从列表中读取第一个项目应该会导致CSV如下所示:
key_header | 0
---------------------------
key0 | key0_value_0
key1 | key1_value_0
读取第二个项目现在应该会产生以下结果:
key_header | 0 | 1
----------------------------------------
key0 | key0_value_0 | key0_value_1
key1 | key1_value_0 | key1_value_1
这是水平直到直到。编写此代码的算法超出了我的范围,我不确定 csv模块是否会因为它似乎假设数据将一次写一行。
您必须首先收集所有'columns',然后写入。您可以通过将所有内容转换为列表列表,然后使用 zip(* columns)
将列的列表转置为行列表:
columns = [['key_header'] + sorted(inputlist [0] .keys())]#第一列
pre>
for i,enumerate(inputlist)中的条目:
columns.append([i] + [列[0] [1:]]中的k的入口[k])
open(outputfilename,'wb')as output:
writer = csv.writer(output)
writer.writerows(zip(* columns))
显示行输出的演示:
> >从pprint import pprint
>>>> inputlist = [{'key0':'key0_value0','key1':'key1_value0'},
... {'key0':'key0_value1','key1':'key1_value1'}]
>>>> columns = [['key_header'] + sorted(inputlist [0] .keys())]#第一列
>>>>对于i,在枚举中输入(inputlist):
... columns.append([i] + [entries [k] for k in columns [0] [1:]])
...
>>>> pprint(zip(* columns))
[('key_header',0,1),
('key0','key0_value0','key0_value1'),
('key1' 'key1_value0','key1_value1')]
Say we are reading data from some source with multiple key-value pairs. Let's use the following list as an example:
[{'key0': 'key0_value0', 'key1': 'key1_value0'}, {'key0': 'key0_value1', 'key1': 'key1_value1'}]
Reading the first item from that list should result in a CSV looking like this:
key_header | 0 --------------------------- key0 | key0_value_0 key1 | key1_value_0
Reading the second item should now result in the following:
key_header | 0 | 1 ---------------------------------------- key0 | key0_value_0 | key0_value_1 key1 | key1_value_0 | key1_value_1
This goes on horizontally until until. The algorithm to write this is beyond me, and I am not sure if the csv module will work since it appears to assume data will be written a row at a time.
解决方案You'll have to first collect all your 'columns', then write. You can do that by converting everything to a list of lists, then use
zip(*columns)
to transpose the list of columns to a list of rows:columns = [['key_header'] + sorted(inputlist[0].keys())] # first column for i, entry in enumerate(inputlist): columns.append([i] + [entry[k] for k in columns[0][1:]]) with open(outputfilename, 'wb') as output: writer = csv.writer(output) writer.writerows(zip(*columns))
Demo showing the row output:
>>> from pprint import pprint >>> inputlist = [{'key0': 'key0_value0', 'key1': 'key1_value0'}, ... {'key0': 'key0_value1', 'key1': 'key1_value1'}] >>> columns = [['key_header'] + sorted(inputlist[0].keys())] # first column >>> for i, entry in enumerate(inputlist): ... columns.append([i] + [entry[k] for k in columns[0][1:]]) ... >>> pprint(zip(*columns)) [('key_header', 0, 1), ('key0', 'key0_value0', 'key0_value1'), ('key1', 'key1_value0', 'key1_value1')]
这篇关于水平写入CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!