-1

I have a list with a dictionary inside containing another list for the key. I would like to have all that information put into a csv. I tried xlwt and csv but I am having a hard time with it.

Here is the list and the dict which is called peopleFood

 {(170, '2017-05-31'): [[0, 3], [1, 2], [2, 3], [3, 1], [4, 2], [5, 1], [6, 2], [7, 
 3], [8, 6], [9, 8], [10, 9], [11, 10], [12, 9], [13, 9], [14, 6], [15, 8], [16, 
 7], [17, 3], [18, 3], [19, 3], [20, 2], [21, 1], [22, 1], [23, 1]]},
 {(176, '2017-05-23'): [[14, 9], [13, 9], [17, 5], [10, 10], [20, 2], [8, 5], [16, 7], [7, 4], [6, 1], [5, 2], [11, 11], [1, 2], [15, 9], [21, 3], [4, 1], [3, 2], [22, 3], [23, 3], [12, 10], [2, 1], [18, 2], [19, 2], [9, 12], [0, 2]]}, 
{(152, '2017-05-31'): [[0, 3], [1, 2], [2, 3], [3, 1], [4, 2], [5, 1], [6, 2], [7, 3], [8, 6], [9, 8], [10, 9], [11, 10], [12, 9], [13, 9], [14, 6], [15, 8], [16, 7], [17, 3], [18, 3], [19, 3], [20, 2], [21, 1], [22, 1], [23, 1]]}

I wont paste the codes since Its mostly all not working. I tried checking out How do I write a Python dictionary to a csv file in the other questions but the dict within the list messes up the codes.

Masnad Nihit
  • 1,986
  • 2
  • 21
  • 40
  • 2
    Possible duplicate of [How do I write a Python dictionary to a csv file?](https://stackoverflow.com/questions/10373247/how-do-i-write-a-python-dictionary-to-a-csv-file) – Software2 Jun 23 '17 at 22:21
  • @Software2 I tried that, dint work, or else I wont be posting the question since it looks pretty simple, the dictionary within the list messes it up – Masnad Nihit Jun 23 '17 at 22:23
  • Why the list of dicts (rather than putting it all in one dict)? – wbadart Jun 23 '17 at 22:27
  • @wilusdaman I am using to get this information from multiple places, so when trying to add it to a dict on how I want, it was crashing so appending it to a list was a good option – Masnad Nihit Jun 23 '17 at 22:28
  • Interesting. I can think of a solution, but it's much cleaner with a list of tuples (which is basically what this is, with the single-items dicts). Would that work with your constraints? – wbadart Jun 23 '17 at 22:31
  • @wilusdaman you mean without having a list, and just having a dictionary? – Masnad Nihit Jun 23 '17 at 22:33
  • Absolutely. That's what I was getting at with my first comment. – wbadart Jun 23 '17 at 22:34
  • Ahh okay, sorry I dint understand ^_^, yea removing that is easy :D Excel is the hard part – Masnad Nihit Jun 23 '17 at 22:35

2 Answers2

1

This is relatively easy to do at the Python side. With your data:

data = [
    {(170, '2017-05-31'): [
        [0, 3], [1, 2], [2, 3], [3, 1], [4, 2], [5, 1], [6, 2], [7, 3], [8, 6], [9, 8],
        [10, 9], [11, 10], [12, 9], [13, 9], [14, 6], [15, 8], [16, 7], [17, 3], [18, 3],
        [19, 3],[20, 2], [21, 1], [22, 1], [23, 1]
    ]},
    {(176, '2017-05-23'): [
        [14, 9], [13, 9], [17, 5], [10, 10], [20, 2], [8, 5], [16, 7], [7, 4], [6, 1],
        [5, 2], [11, 11], [1, 2], [15, 9], [21, 3], [4, 1], [3, 2], [22, 3], [23, 3],
        [12, 10], [2, 1], [18, 2], [19, 2], [9, 12], [0, 2]
    ]},
    {(152, '2017-05-31'): [
        [0, 3], [1, 2], [2, 3], [3, 1], [4, 2], [5, 1], [6, 2], [7, 3], [8, 6], [9, 8],
        [10, 9], [11, 10], [12, 9], [13, 9], [14, 6], [15, 8], [16, 7], [17, 3], [18, 3],
        [19, 3], [20, 2], [21, 1], [22, 1], [23, 1]
    ]}
]

all you need to do is:

with open("test.csv", "wb") as f:  # on Python 3.x use "w" mode and newline='' instead
    writer = csv.writer(f)
    for category in data:  # get our category
        for header, rows in category.iteritems():  # use category.items() on Python 3.x
            writer.writerow(header)  # add the category/date header
            writer.writerow(["People", "Food"])  # add the mandatory sub-header
            writer.writerows(rows)  # write the rest of the data

To get your CSV... But loading such CSV is a whole other topic.

zwer
  • 24,943
  • 3
  • 48
  • 66
  • Hi, Zwer thanks for the answer, I removed the [ from the data so now its just a dictionary, can the same instructions you gave be applied? – Masnad Nihit Jun 23 '17 at 23:11
  • @MasnadNihit - in that case remove the first loop (`for category in data`) and directly call .iteritems() on your data. – zwer Jun 23 '17 at 23:13
  • okay, I first tried it with everything how it is , but now I am getting this error for header, rows in category.iteritems(): # use category.items() on Python 3.x AttributeError: 'dict' object has no attribute 'iteritems' – Masnad Nihit Jun 23 '17 at 23:14
  • @MasnadNihit - as the comment says, use `category.items()` instead of `category.iteritems()` if you're using Python 3.x. – zwer Jun 23 '17 at 23:15
  • Another new error, TypeError: a bytes-like object is required, not 'str' on line writer.writerow(header) – Masnad Nihit Jun 23 '17 at 23:18
  • @MasnadNihit - you have another comment regarding Python 3.x at the very beginning of the code snippet - when opening a file for writing you should use `"w"` mode instead and you should add an additional argument `newline=""` to the `open()` call, i.e. `with open("test.csv", "w", newline="") as f: ...` – zwer Jun 23 '17 at 23:21
  • Yea, I din't read that at all, sorry, I was fussing over this problem for the last 4-5 hours, so now I am just stressed, Thanks! it worked out!!!!! Grateful for the help – Masnad Nihit Jun 23 '17 at 23:23
0
with open('filename', 'w') as buffer:
    data = [{(...): [...]}, {...}, ...]

    keys = ('people', 'food')

    writer = csv.DictWriter(buffer, fieldnames=keys)

    for record in data:
        first_row = {i: '' for i in record.keys()}
        writer.writerow(first_row)
        writer.writeheader()
        rows = [dict(zip(keys, row)) for row in record.items()[0]]
        writer.writerows(rows)
Dan
  • 1,874
  • 1
  • 16
  • 21