人生若只如初见

WXY

Python读取Excel数据转成List

2023-10-08

环境: 系统linux 编辑器pycharm python版本3.11

excel文件内容:

2023-10-08_17-00.png

代码

from openpyxl import load_workbook

if __name__ == '__main__':
    workbook = load_workbook('/home/wxy/Desktop/b.xlsx')
    # 选中工作簿中的表单
    sh = workbook.active

    # 按行读取,每一行的数据放到一个元组中
    res = list(sh.rows)

    # 表头
    title = [i.value for i in res[0]]

    myList = list()

    # 遍历除第一行外的其他行
    for item in res[1:]:
        it = [i.value for i in item]
        # 打包成字典
        line = dict(zip(title, it))
        myList.append(line)

    # 遍历查看数据
    for i in myList:
        print(i.get("到站"))

执行结果

2023-10-08_16-57.png