python 动态导入示例

from openpyxl import load_workbook

def get_title():
    return {"facets": [{"facet_id": "1", "facet_name": "主流交付场景",
                        "cards": [{"card_id": "1", "card_name": "卡片1"},
                                  {"card_id": "2", "card_name": "卡片2"}]
                        },
                       {"facet_id": "2", "facet_name": "基本能力",
                        "cards": [{"card_id": "3", "card_name": "卡片3"},
                                  {"card_id": "4", "card_name": "卡片4"}
                                  ]}]}


def test_import():
    index_map = {}
    index_map[2] = "staff_id"
    index_map[3] = "staff_name"

    index_map_start = 4
    title = get_title()
    for index1, facet_item in enumerate(title['facets']):
        for index2, card_item in enumerate(facet_item["cards"]):
            index_map[index_map_start] = f'facet{facet_item["facet_id"]}_card{card_item["card_id"]}'
            index_map_start += 1

    # 读取已存在的excel工作薄
    wb = load_workbook('test.xlsx')
    sheet = wb.active

    # 导入的结果数据
    result = []
    # 从第3行开始读
    for row_index in range(3, sheet.max_row):
        # 读完的行数据存放
        row_data = {}
        # 从第2列开始读
        for col_index in range(2, index_map_start):
            filed_name = index_map[col_index]
            row_data[index_map[col_index]] = sheet.cell(row_index, col_index).value
        result.append(row_data)

    return result


if __name__ == '__main__':
    result = test_import()
    print(result)

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_29484773

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值