I would like to add data to a spreadsheet for analysis at a later date. I have tried a few different approaches using for loops, but they always come up short or with errors. I am new to python, but I'm trying to learn as I build. What I need to do is take the dictionary 'dict_items' and using the keys insert the data to an existing excel spreadsheet. However, if i go item by item I get only one item per row for some of the columns and more items for other columns. This is because there is more data in some keys than others. What I need is for the set of data for the keys that have only one item to copy that one item for all the other rows until the last item for the key with the most items is copied. I've put an example output below if that will help make sense of what I am trying to do.
For example this is the dictionary and one of the for loops I've tried:
dict_items = {'most items':['1','2','3','4','5'], 'some items':['item1','item2','item3','item4','item5'], 'one item': 'banana'} import xlsxwriter workbook = xlsxwriter.Workbook('data.xlsx') worksheet = workbook.add_worksheet() row = 0 col = 0 dict_items = {'most items':['1','2','3','4','5'], 'some items':['item','item','item','item','item'], 'one item': 'banana'} for key in d.keys(): row += 1 worksheet.write(row, col, key) for item in d[key]: worksheet.write(row, col + 1, item) row += 1 This works to add the data to a spreadsheet, but does not work to ensure the data is even along each row.
The spreadsheet should look something like this if the data is input correctly:
| most items | some items | one item | | 1 | item1 | banana | | 2 | item2 | banana | | 3 | item3 | banana | | 4 | item4 | banana | | 5 | item5 | banana | Hopefully the output example will give you a better idea of what I'm trying to do.
