Cloud Stack Ninja

I read one xlsx file in this i have two sheets and i want store in a dictionary of tuples but at the end of the tuples, I got extra comma.

my output:
data_dict = {"sheet1":[('A',), ('B',), ('C',), ('D',)],"sheet2":[('A',), ('B',), ('C',), ('D',)]}

The correct output I want:
data_dict = {"sheet1":[('A'), ('B'), ('C'), ('D')],"sheet2":[('A'), ('B'), ('C'), ('D')]}

My current code is

import openpyxl
wb = load_workbook(xlsx_filename)
#print(wb)
data_dict = {}
for sheet in wb:
    #print(sheet)

    data_dict[sheet.title] = []
    for row in sheet.rows:
        #print(sheet.rows)
        row_wise_data = tuple([cell.value for cell in row])
        data_dict[sheet.title].append(row_wise_data)
#print(data_dict)  # result in dictionary

return data_dict
wb.close()


Read more here: https://stackoverflow.com/questions/64405881/i-read-one-xlsx-file-in-this-i-have-two-sheets-and-i-want-store-in-a-dictionary

Content Attribution

This content was originally published by abhi spdy at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: