Skip to main content
 首页 » 编程设计

python之在 Python 中将字典解析为电子表格

2024年10月24日15yjmyzz

我有一些类似格式的数据:

James has 6 jeans, 10 shirts, 5 shoes, 6 ties 
Nick has 8 jeans, 4 shirts, 3 shoes, 4 ties 
Adam has 2 jeans, 3 shirts, 5 shoes, 1 tie 
John has 6 jeans, 5 shirts, 10 shoes, 3 ties 

使用 collections.defaultdict(list),我生成了以下格式的字典:

{James: [[Jeans, 6], 
         [Shirts, 10], 
         [Shoes, 5], 
         [Ties, 6]], 
 Nick: [[Jeans, 8], 
        [Shirts, 4], 
        [Shoes, 3], 
        [Ties, 4]], 
 Adam: [[Jeans, 2], 
        [Shirts, 3], 
        [Shoes, 5], 
        [Ties, 1]], 
 John: [[Jeans, 6], 
        [Shirts, 5], 
        [Shoes, 10], 
        [Ties, 3]]} 

我试图在 cvs 电子表格中获取此输出:

Accesories  James    Nick       Adam     John 
 
Jeans         6       8           2        6 
 
Shirts       10       4           3        5 
 
Shoes        5        3           5        10 
 
Ties         6        4           1         3 

在构建字典之前进行了一些编码之后,这里是我到目前为止尝试使用创建的字典生成与上述格式相同的电子表格的代码。 cvs ,itertools 模块已经导入。

with open(outputcsv, 'w') as w: 
    writer = csv.writer(w, delimiter=',', lineterminator='\n') 
    writer.writerow(dict.keys()) 
 
    zipped = itertools.izip_longest(*dict.values()) 
    writer.writerows(list(zipped)) 

这让我非常接近预期的解决方案,但又不完全是。

请您参考如下方法:

作为一个字典的字典,而不是一个列表列表的字典,这会更容易。

假设:

w={'James': {'Jeans': 6, 
     'Shirts': 10, 
     'Shoes': 5, 
     'Ties': 6,}, 
 'Nick': {'Jeans': 8, 
    'Shirts': 4, 
    'Shoes': 3, 
    'Ties': 4}, 
 'Adam': {'Jeans': 2, 
    'Shirts': 3, 
    'Shoes': 5, 
    'Ties': 1}, 
 'John': {'Jeans': 6, 
    'Shirts': 5, 
    'Shoes': 10, 
    'Ties': 3, 
    'Belts': 1}} 

注意 John 有腰带而其他人没有,这是一个字典的字典,而不是两个元素列表的字典。

首先,让我们列出一份(好吧,一套...)任何人拥有的所有配件:

accessories=set()     
for nam, di in w.items(): 
    for k in di: 
        accessories.add(k)   
>>> accessories 
set(['Ties', 'Belts', 'Jeans', 'Shirts', 'Shoes']) 

现在可以直接以您的格式创建这些项目的 CSV:

import csv 
 
with open('/tmp/so.csv', 'w') as f: 
    writer=csv.writer(f) 
    writer.writerow(['accessories']+w.keys())  
    for item in accessories: 
        li=[item] 
        for nam in w: 
            li.append(w[nam].get(item, 0)) 
        writer.writerow(li)    

现在查看文件:

accessories,James,John,Adam,Nick 
Ties,6,3,1,4 
Belts,0,1,0,0 
Jeans,6,6,2,8 
Shirts,10,5,3,4 
Shoes,5,10,5,3 

如果您坚持使用每两个元素的列表列表的字典(就像您在示例中所做的那样),您可以转换它们:

>>> w={'James': [['Jeans', 6], 
...      ['Shirts', 10], 
...      ['Shoes', 5], 
...      ['Ties', 6]], 
...  'Nick': [['Jeans', 8], 
...     ['Shirts', 4], 
...     ['Shoes', 3], 
...     ['Ties', 4]], 
...  'Adam': [['Jeans', 2], 
...     ['Shirts', 3], 
...     ['Shoes', 5], 
...     ['Ties', 1]], 
...  'John': [['Jeans', 6], 
...     ['Shirts', 5], 
...     ['Shoes', 10], 
...     ['Ties', 3], 
...     ['Belts', 1]]} 
>>> {k:dict(LoL) for k, LoL in w.items()} 
{'James': {'Ties': 6, 'Jeans': 6, 'Shirts': 10, 'Shoes': 5}, 'John': {'Ties': 3, 'Belts': 1, 'Jeans': 6, 'Shirts': 5, 'Shoes': 10}, 'Adam': {'Ties': 1, 'Jeans': 2, 'Shirts': 3, 'Shoes': 5}, 'Nick': {'Ties': 4, 'Jeans': 8, 'Shirts': 4, 'Shoes': 3}}