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()
相关推荐
you鬰3 分钟前
datawhale--llm-algo-leetcode5️⃣
python·datawhale
λqaq723 分钟前
MySQL 数据库基础(2)约束、用户权限、远程连接、外键与 TCP/UDP 理解
linux·数据库·python·tcp/ip·mysql
人工干智能26 分钟前
科普:pandas 时间偏移别名:ts.resample(“5Min“).sum()
python·pandas
开源量化GO35 分钟前
最新AI辅助量化表达:先理清规则,再按需求选工具
人工智能·python
Zane199442 分钟前
为什么 del 一个对象有时立刻消失,有时却纹丝不动?一文讲透引用计数与垃圾回收
后端·python
编码浪子1 小时前
Rust 智能指针深度实战:Box/Rc/Arc/RefCell 选型决策与内存管理避坑
开发语言·后端·rust
yichudu1 小时前
Python 装饰器 decorator
开发语言·python
电商API_180079052471 小时前
速卖通商品采集API技术文章
java·开发语言·c++·api·跨境电商·商品详情
一晌小贪欢1 小时前
Python办公14:批量解压文件夹内的所有 ZIP/RAR 并自动分类
开发语言·python·excel·数据可视化·python办公
郑州光合科技余经理1 小时前
海外版外卖系统架构:订单怎么流转、权限怎么分
java·开发语言·前端·系统架构·uni-app·php·ai编程