简体   繁体   English

从元组列表字典创建CSV文件

[英]Create CSV file from dictionary of list of tuples

I would like to take this dictionary I have: 我想拿这本字典:

    table = {2015-11-10:[('pear', 22), ('banana', 50)], 
    2015-11-13:[('banana', 22), ('apple', 50), ('strawberry', 30), ('pear', 35), ('orange', 2), ('mango', 76)]
    2015-11-12:[('banana', 22), ('orange', 50), ('mango', 30), ('strawberry', 35)]
    }

    with open ('transaction.csv', 'wb') as f:
        w = csv.writer(f, delimiter = ',')
        w.writerow(['transaction_date', 'fruits', 'quantity'])
        for k,v in table.items():
            w.writerow([k,v])

What I want to be printed is that each key, aka date has only one pair of tuples such that there are only three columns. 我要打印的是每个键(也称为日期)只有一对元组,因此只有三列。 However I keep having it be printed out as: 但是我一直将其打印为:

    2015-11-10,"[('pear', 22), ('banana', 50)]"
    2015-11-13,"[('banana', 22), ('apple', 50), ('strawberry', 30), ('pear', 35), ('orange', 2), ('mango', 76)]"
    2015-11-12,"[('banana', 22), ('orange', 50), ('mango', 30), ('strawberry', 35)]"

Instead of this: 代替这个:

    2015-11-10,pear,22
    2015-11-10,banana,50
    2015-11-13,banana,22

And so on. 等等。

Any help would definitely be appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

The data structure is nested. 数据结构是嵌套的。 You need an inner loop over the fruits and quantities: 您需要对水果和数量进行内部循环

import csv

table = {"2015-11-10": [('pear', 22), ('banana', 50)],
         "2015-11-13": [('banana', 22), ('apple', 50), ('strawberry', 30), ('pear', 35), ('orange', 2), ('mango', 76)],
         "2015-11-12": [('banana', 22), ('orange', 50), ('mango', 30), ('strawberry', 35)]
         }

with open('transaction.csv', 'wb') as f:
    w = csv.writer(f, delimiter=',')
    w.writerow(['transaction_date', 'fruits', 'quantity'])

    for transaction_date, fruits in table.items():
        for fruit, quantity in fruits:
            w.writerow([transaction_date, fruit, quantity])

Here is what I have inside the transaction.csv after running the code: 这是运行代码后在transaction.csv的内容:

transaction_date,fruits,quantity
2015-11-10,pear,22
2015-11-10,banana,50
2015-11-13,banana,22
2015-11-13,apple,50
2015-11-13,strawberry,30
2015-11-13,pear,35
2015-11-13,orange,2
2015-11-13,mango,76
2015-11-12,banana,22
2015-11-12,orange,50
2015-11-12,mango,30
2015-11-12,strawberry,35

I think this will accomplish your objective. 我认为这将实现您的目标。 You need to also iterate over the value pairs associated with each date key: 您还需要遍历与每个日期键关联的值对:

import csv

table = {
    '2015-11-10':[('pear', 22),
                  ('banana', 50)], 
    '2015-11-13':[('banana', 22),
                  ('apple', 50),
                  ('strawberry', 30),
                  ('pear', 35),
                  ('orange', 2),
                  ('mango', 76)],
    '2015-11-12':[('banana', 22),
                  ('orange', 50),
                  ('mango', 30),
                  ('strawberry', 35)]
    }

with open ('transaction.csv', 'wb') as f:
        w = csv.writer(f, delimiter = ',')
        w.writerow(['transaction_date', 'fruits', 'quantity'])
        for date, pairs in table.items():
            for pair in pairs:
                w.writerow([date, pair[0], pair[1]])

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

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