2

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.

1 Answer 1

2

The best solution is to transform your dictionary to a pandas.DataFrame by installing pandas (pip install pandas)

import pandas as pd dict_items = {'most items':['1','2','3','4','5'], 'some items':['item','item','item','item','item'], 'one item': 'banana'} #create a DataFrame df = pd.DataFrame(dict_items) #contatenate a column with the index df['some items'] = df['some items'] + df['most items'] #create the excel file df.to_excel("example.xlsx", index=False) 

output

enter image description here

Otherwise

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 list(dict_items.keys())[0:3]: worksheet.write(row, col, key) #print(str(row) + " " + str(col) + " "+ str(key)) for item in dict_items[key]: row += 1 if key == 'most items': #print(str(row) + " " + str(col) + " "+ str(key)) worksheet.write(row, col, item) #print(item) if key == "some items": worksheet.write(row, col, item+str(row)) if key == "one item": if row <= len(dict_items.get("most items")): worksheet.write(row, col, dict_items[key]) #print(dict_items[key]) col +=1 row = 0 workbook.close() 
Sign up to request clarification or add additional context in comments.

4 Comments

Does that answer your question?
This works perfectly for what I'm trying to do. The only problem I ran into is that I do have one column that I need to print only once in the first row for the set of data. I have hundreds of sets of data and each one is in its own dictionary like the example. Most of the items can be copied and that gives me the outcome I need, but there is one key in each set that I want to only print once per dictionary. I haven't tried it yet, but I believe the second example you gave will get me closer to the outcome I'm looking for since the code uses the keys themselves to create the headers in excel.
Hi @JxnDistro let me know if you have issues when after you tried the code.
The dataframe solution worked out pretty well. I think I can solve my problem by adding a counter for the 'most items' column and for each row prior to the last add ' ' to the column with only one item. Then this would print the single item on the last row of the set of data and begin again with ' ' for the next set. I'm still working on the written logic, but it's my plan.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.