excel - how to create range from a list of python dicts? -
i'm using openpyxl evaluate worksheet. there 1 row on worksheet contains column_group names in merged cells. created list of dicts column_group key , column number value
col_groups= [{u'layer': 1}, {u'single ended': 17},\ {u'single ended': 22}, {u'edge coupled': 27}]
now want create group_name:(start_column:end_column) dictionary using col_groups
this closest i've been able get.
group_cols = [] x, d in enumerate(col_groups): try: group_cols.append({col_groups[x].keys()[0]:(col_groups[x].values()[0],(col_groups[(x+1)].values()[0] - 1))}) except: group_cols.append({col_groups[x].keys()[0]:(col_groups[x].values()[0],tap_sh.max_column)})
typing group_cols @ python shell prompt gives:
[{u'layer': (1, 16)}, {u'single ended': (17, 21)}, {u'single ended': (22, 26)}, {u'edge coupled': (27, 33)}]
the output looks ok method feels bit hackish- suggestions more pythonic appreciated
a dictionary wrong structure, tuple (name, idx) better, can work it.
# define sort key (python 3 compatible) def sort_key(value): "sort value" return list(value.values())[-1] ordered_groups = [] max_column = 34 group in sorted(col_groups, key=sort_key, reverse=true): key, begin = list(group.items())[0] end = max_column - 1 max_column = begin ordered_groups.append((key, (begin, end))) dict(ordered_groups)
Comments
Post a Comment