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()
相关推荐
cup117 小时前
[技术复盘] Windows Python 打包实战:Nuitka 环境踩坑总结与 CI 自动化构建全指南
python·ai·环境变量·ci·nuitka·skill
aqi009 小时前
15天学会AI应用开发(七)有了大模型为什么还要引入RAG
人工智能·python·大模型·ai编程·ai应用
金銀銅鐵10 小时前
用 Python 实现 Take-Away 游戏
python·游戏
copyer_xyf11 小时前
Agent 流程编排
后端·python·agent
copyer_xyf12 小时前
Agent RAG
后端·python·agent
copyer_xyf12 小时前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf12 小时前
Agent 记忆管理
后端·python·agent
星云穿梭1 天前
用Python写一个带图形界面的学生管理系统——完整教程
python
金銀銅鐵1 天前
用 Pygame 实现 15 puzzle
python·数学·游戏