python读取xlsx格式的excel

liftword2个月前 (04-01)技术文章21

读取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()

相关文章

Python读取Excel文件的方法

方法一:读excel文件单元格数据import xlrdbook = xlrd.open_workbook('fruit.xlsx')print('sheet页名称:',book.sheet_name...

python怎么读取excel文件

python怎么读取excel文件?1.首先说明我是使用的python3.5,我的office版本是2010,首先打开dos命令窗,安装必须的两个库,命令是:12pip3 install xlrdPi...

python读取Excel表格

python读取Excel文件有一个专门的第三方库xlrd,我们可以安装一下pip install xlrd有关Excel文件的读取如下path = "./test.xls" '''open_wor...

python除了熟悉的pandas,openpyxl库也很方便的读取Excel表内容

学习目录了解下电脑中的excel表格文件格式安装openpyxl库使用openpyxl库读取表格内容1 先准备一个表格‘python.xlsx’,表格中包含如下几个sheet页2 导入openpyxl...

Python之Pandas使用系列(八):读写Excel文件的各种技巧

介绍:我们将学习如何使用Python操作Excel文件。我们将概述如何使用Pandas加载xlsx文件以及将电子表格写入Excel。如何将Excel文件读取到Pandas DataFrame:和前面的...

Python读取与写入Excel模块:openpyxl

之前我们介绍过几个模块:xlrd、xlwt、XLsxWriter等,但是这些模块要门只能只读,要么只能只写,那么有没有即可读,也可写的模块,答案是肯定的,如pandas、openpyxl、文2com等...