python读取xlsx格式的excel

读取excel表格数据最好用的还是pandas库

首先是安装pandas

复制代码
pip install pandas

引入pandas

复制代码
import pandas as pd

读取excel,xlsx格式数据

复制代码
# 读取xlsx格式的数据
def readexcel():
    df = pd.read_excel("./test.xlsx",header=None)
    df.columns = df.iloc[4]

    data = []
    for index, row in df.iterrows():
        if index < 5:
            continue
        line = (generate_random_string(20),row['序号'],row['姓名'],row['性别'],row['年龄'])
        data.append(line)

    return data

说明

1、读取表格数据,header=None 因为pandas默认打开第一行为字段名称,可作为下标直接读取数据,加上header参数后,将不默认第一行为字段名称行。

复制代码
df = pd.read_excel("./后续监管查询列表.xlsx",header=None)

2、我的表格第5行为字段名称行,以下代码配置字段名称行

复制代码
 df.columns = df.iloc[4]

3、按字段名称获取数据

复制代码
 line = (generate_random_string(20),row['序号'],row['姓名'],row['性别'],row['年龄'])

最后调用方法就ok了。

复制代码
data = readexcel()
相关推荐
Warson_L1 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅1 天前
海天线算法的前世今生
python·计算机视觉
韩师傅1 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L1 天前
LangGraph的MessageState and HumanMessage
python
韩师傅1 天前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L1 天前
python的类&继承
python
Warson_L1 天前
类型标注/type annotation
python
ThreeS1 天前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵1 天前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏