• python 使用xlsx和pandas处理Excel表格


    目录

    一、使用xls和xlsx处理Excel表格

    1.1 用openpyxl模块打开Excel文档,查看所有sheet表

    1.2 通过sheet名称获取表格

    1.3 获取活动表的获取行数和列数

    ◼ 读取xlsx文件错误:xlrd.biffh.XLRDError: Excel xlsx file; not supported

    二、使用pandas读取xlsx

    2.1 读取数据

    2.2 使用pandas查找两个列表中相同的元素

    ◼ 解决ValueError: Excel file format cannot be determined, you must specify an engine manually.

    ◼ 解决but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

    ◼ 解决MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.


    一、使用xls和xlsx处理Excel表格

    xls是excel2003及以前版本所生成的文件格式;
    xlsx是excel2007及以后版本所生成的文件格式;
    (excel 2007之后版本可以打开上述两种格式,但是excel2013只能打开xls格式);

    1.1 用openpyxl模块打开Excel文档,查看所有sheet表

    openpyxl.load_workbook()函数接受文件名,返回一个workbook数据类型的值。这个workbook对象代表这个Excel文件,这个有点类似File对象代表一个打开的文本文件。  

    1. workbook = xlrd2.open_workbook("1.xlsx") # 返回一个workbook数据类型的值
    2. sheets = workbook.sheet_names()
    3. print(sheets)
    4. # 结果:
    5. # ['Sheet1', 'Sheet2']

    或者

    1. workbook = openpyxl.load_workbook("1.xlsx") # 返回一个workbook数据类型的值
    2. print(workbook.sheetnames) # 打印Excel表中的所有表
    3. # 结果:
    4. # ['Sheet1', 'Sheet2']

    1.2 通过sheet名称获取表格

    1. workbook = openpyxl.load_workbook("数据源总表(1).xlsx") # 返回一个workbook数据类型的值
    2. print(workbook.sheetnames) # 打印Excel表中的所有表
    3. sheet = workbook['Sheet1'] # 获取指定sheet表
    4. print(sheet)
    5. # 结果:
    6. # ['Sheet1', 'Sheet2']
    7. #

    1.3 获取活动表的获取行数和列数

    方法1:自己写一个for循环

    方法2:使用

    • sheet.max_row 获取行数
    • sheet.max_column 获取列数
    1. workbook = openpyxl.load_workbook("数据源总表(1).xlsx") # 返回一个workbook数据类型的值
    2. print(workbook.sheetnames) # 打印Excel表中的所有表
    3. sheet = workbook['1、基本情况'] # 获取指定sheet表
    4. print(sheet)
    5. print('rows', sheet.max_row, 'column', sheet.max_column) # 获取行数和列数

    ◼ 读取xlsx文件错误:xlrd.biffh.XLRDError: Excel xlsx file; not supported

    运行代码时,会出现以下报错。

    xlrd.biffh.XLRDError: Excel xlsx file; not supported

    (1)检查第三方库xlrd的版本:

    我这里的版本为xlrd2.0.1最新版本,问题就出在这里,我们需要卸载最新版本安装旧版本,卸载安装过程如下。

    (2)在File-Settings下的Project-Python Interpreter中重新按照旧版本xlrd2,

    按照上述步骤卸载xlrd后再安装xlrd2后,

    可以看到错误解决了。

    二、使用pandas读取xlsx

    pyCharm pip安装pandas库,请移步到python之 pyCharm pip安装pandas库失败_水w的博客-CSDN博客_pandas安装失败

    2.1 读取数据

    1. import pandas as pd
    2. #1.读取前n行所有数据
    3. df1=pd.read_excel('d1.xlsx')#读取xlsx中的第一个sheet
    4. data1=df1.head(10) #读取前10行所有数据
    5. data2=df1.values #list【】 相当于一个矩阵,以行为单位
    6. #data2=df.values() 报错:TypeError: 'numpy.ndarray' object is not callable
    7. print("获取到所有的值:\n{0}".format(data1))#格式化输出
    8. print("获取到所有的值:\n{0}".format(data2))
    9. #2.读取特定行特定列
    10. data3=df1.iloc[0].values #读取第一行所有数据
    11. data4=df1.iloc[1,1] #读取指定行列位置数据:读取(1,1)位置的数据
    12. data5=df1.iloc[[1,2]].values #读取指定多行:读取第一行和第二行所有数据
    13. data6=df1.iloc[:,[0]].values #读取指定列的所有行数据:读取第一列所有数据
    14. print("数据:\n{0}".format(data3))
    15. print("数据:\n{0}".format(data4))
    16. print("数据:\n{0}".format(data5))
    17. print("数据:\n{0}".format(data6))
    18. #3.获取xlsx文件行号、列号
    19. print("输出行号列表{}".format(df1.index.values)) #获取所有行的编号:0、1、2、3、4
    20. print("输出列标题{}".format(df1.columns.values)) #也就是每列的第一个元素
    21. #4.将xlsx数据转换为字典
    22. data=[]
    23. for i in df1.index.values: #获取行号的索引,并对其遍历
    24. #根据i来获取每一行指定的数据,并用to_dict转成字典
    25. row_data=df1.loc[i,['id','name','class','data','score',]].to_dict()
    26. data.append(row_data)
    27. print("最终获取到的数据是:{0}".format(data))
    28. #iloc和loc的区别:iloc根据行号来索引,loc根据index来索引。
    29. #所以1,2,3应该用iloc,4应该有loc

    读取特定的某几列的数据:

    1. import pandas as pd
    2. file_path = r'int.xlsx' # r对路径进行转义,windows需要
    3. df = pd.read_excel(file_path, header=0, usecols=[3, 4]) # header=0表示第一行是表头,就自动去除了, 指定读取第3和4列

     

    2.2 使用pandas查找两个列表中相同的元素

    解决:查找两个列表中相同的元素,可以把列表转为元祖/集合,进行交运算。

    1. import pandas as pd
    2. file_path = r'int.xlsx' # r对路径进行转义,windows需要
    3. df = pd.read_excel(file_path, header=0, usecols=[3, 4]) # header=0表示第一行是表头,就自动去除了, 指定读取第3和4列
    4. i, o = list(df['i']), list(df['o'])
    5. in_links, out_links = [], []
    6. a = set(in_links) # 转成元祖
    7. b = set(out_links)
    8. c = (a & b) # 集合c和b中都包含了的元素
    9. print(a, '\n', b)
    10. print('两个列表中相同的元素是:', list(c))

    ◼ 解决ValueError: Excel file format cannot be determined, you must specify an engine manually.

    报错:我在使用python的pandas读取表格的数据,但是报错了,

    1. import pandas as pd
    2. file_path = 'intersection.xlsx' # r对路径进行转义,windows需要
    3. df = pd.read_excel(file_path, header=0, usecols=[0]) # header=0表示第一行是表头,就自动去除了, 指定读取第1列
    4. print(df)

    问题:问题在于原表格格式可能有些问题。

    解决:最直接的办法就是把表格的内容复制到一个自己新建的表格中,然后改成之前表格的路径,

    然后再安装这个openpyxl第三方库。

     pip install openpyxl
    

    重新运行代码,

    ok,问题解决。

    ◼ 解决but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

    报错

    but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

    问题:xxx文件里有中文字符。

    解决:在py文件的代码第一行 加上,

    #  -*-coding:utf8 -*-

    ◼ 解决MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.

    报错:在使用pandas读取文件时,显示错误。

    MatplotlibDeprecationWarning: Support for FigureCanvases without a required_interactive_framework attribute was deprecated in Matplotlib 3.6 and will be removed two minor releases later.

    问题:matplotlib3.2以后就把mpl-data分离出去了 。

    解决:卸载原来的版本,安装3.1版本。

    1. pip uninstall matplotlib # 卸载原来的版本
    2. pip install matplotlib==3.1.1 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com # 安装3.1版本

  • 相关阅读:
    统计学习方法学习笔记-07-支持向量机03
    单链表的基本操作
    HDLBits-Lfsr32
    MySQL底层原理
    网络&信息安全:11个常见漏洞类型汇总
    RAID0_RAID1_RAID10_RAID5各需几块盘才可组建
    dotnet-cnblog|迁移Gitee图床图片或上传本地图片到博客园中
    数据监测全过程——采集、清洗、分析
    阴影text-shadow和box-shadow详解
    8.8本周总结
  • 原文地址:https://blog.csdn.net/qq_45956730/article/details/126976482