I am trying to create a following dictionary. I am reading data from excel
which has data in the following format:
Sl no: Name Thickness Length Material Width Quantity Side
It has 20 rows of data.
The dictionary for the 20 rows, I would like to make is
Data_Dict = [
{ 'Name': 'X', 'Length': '10' , 'Width': '5', 'Quantity': 2 'Area': 50};
{ 'Name': 'Y', 'Length': '20' , 'Width': 10', 'Quantity': 1 'Area': 200};
.
.
.
.
till 20 rows
];
I would like to add another key 'Area' as shown above. I used the following
code(s):
using openpyxl:
d={}
for i in range(3,sheet.max_row+1):
#for j in range(3,9):
#for k in range(0,5):
Name = sheet.cell(row= i,column=3).value
Length =sheet.cell(row =i,column=6).value
Breadth= sheet.cell(row=i,column=7).value
Quantity = sheet.cell (row=i,column=8).value
Area = sheet.cell(row
=i,column=6).value*sheet.cell(row=i,column=7).value
d[Name]= Length,Breadth,Quantity,Area
which gave an output like:
['X': (10, 5, 2, 50), 'Y': (20, 10, 1, 2232600), 'Z': (5, 2, 1, 10),
.............]
Another code using xlrd:
keys = [sheet.cell(2, col_index).value for col_index in range(0,8)]
print (keys)
dict_list = []
d = {}
for row_index in range(1, xl_sheet.nrows):
for col_index in range(0,8):
d = {keys[col_index]: xl_sheet.cell(row_index, col_index).value
for col_index in range(0,8)}
dict_list.append(d)
print (dict_list)
which did not have the area and neither the output was little messy.
The main purpose is to sort the dictionary based on different criteria like
Length or Area. I think the first one may be easier to sort. However if there
are any better way to represent the dictionary and the code so that it can be
sorted later based on different attributes please feel free to suggest.
Thanks,
Atri
--
https://mail.python.org/mailman/listinfo/python-list