Here is sample implementation (actually two, slightly different alternatives) how to read and sort the data. Hope it will kick-start you
Of course plenty can be improved.
Of course plenty can be improved.
from collections import defaultdict from collections import namedtuple # used in OPTION 2 from openpyxl.reader.excel import load_workbook filename = 'Example.xlsx' wb = load_workbook(filename) ws = wb.active # read header and all data in two variables -> header and data # Note the asteriks in frot of data # This way data will hold everything except the first element in ws.values header, *data = ws.values # OPTION 1 print('\n\nOPTION 1 output') # parse data into groups groups = defaultdict(list) for row in data: group = row[-1][-1] groups[group].append(row) #print each group in desired order for group, rows in sorted(groups.items()): print('\n\nGroup {}\n---------------\n'.format(group)) for row in sorted(rows, key=lambda x: x[-1][:-1]): print(row) # OPTION 2 print('\n\nOPTION 2 output') # parse data into groups groups = defaultdict(list) # define namedtuple fields = ('cust', 'first_name', 'last_name', 'full_name', 'reorder') Record = namedtuple('Record', fields) for row in data: # parse each row into Record namedtuple record = Record(*row) group = record.reorder[-1] # last char in the recorder property in the record groups[group].append(record) #print each group in desired order for group, records in sorted(groups.items()): print('\n\nGroup {}\n---------------\n'.format(group)) for record in sorted(records, key=lambda rec: rec.reorder[:-1]): print(record)OUTPUT:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs