def 将一个列表中的几个字典改成二维列表(original_list,headersToRemove_list):# 初始化一个列表用于存储遇到的键,保持顺序ordered_keys = []# 遍历data中的每个字典,添加其键到ordered_keys,如果该键还未被添加for d in original_list:for key in d.keys():if key not in ordered_keys:ordered_keys.append(key)if len(headersToRemove_list)!=0:# 使用列表推导式过滤掉需要删除的项目newTitle_list = [item for item in ordered_keys if item not in headersToRemove_list]else:newTitle_list=ordered_keysresult = [newTitle_list]# 对于每个字典,按顺序添加值,没有的键用空字符串填充for d in original_list:row = [str(d.get(key,'')) for key in newTitle_list]result.append(row)return resultoriginal_list=[{'customer_id': '1',"pick_group_name":"默认","trade_id":63783},{'customer_id': '2',"trade_id":6},{'customer_id': '3',"pick_group_name":"你好","trade_id":666},{'customer_id': '4',"pick_group_name":"北京","trade_id":63,"shop_platform_id":23}]print(将一个列表中的几个字典改成二维列表(original_list,['trade_id']))
写入excel后的结果