如何利用Python处理excel表格中的数据

作者:行走的算法 时间:2022-10-27 03:32:38 

一、基础、常用方法

1. 读取excel

1、导入模块:

import xlrd

2、打开文件:

x1 = xlrd.open_workbook("data.xlsx")

3、获取sheet:

sheet是指工作表的名称,因为一个excel有多个工作表

如何利用Python处理excel表格中的数据


获取所有sheet名字:x1.sheet_names()

获取sheet数量:x1.nsheets

获取所有sheet对象:x1.sheets()

通过sheet名查找:x1.sheet_by_name("test”)

通过索引查找:x1.sheet_by_index(3)

# -*- coding:utf-8 -*-

import xlrd
import os

filename = "demo.xlsx"
filePath = os.path.join(os.getcwd(), filename)

print filePath

# 1、打开文件
x1 = xlrd.open_workbook(filePath)

# 2、获取sheet对象
print 'sheet_names:', x1.sheet_names()  # 获取所有sheet名字
print 'sheet_number:', x1.nsheets        # 获取sheet数量
print 'sheet_object:', x1.sheets()       # 获取所有sheet对象
print 'By_name:', x1.sheet_by_name("test")  # 通过sheet名查找
print 'By_index:', x1.sheet_by_index(3)  # 通过索引查找

输出:

sheet_names: [u' plan', u'team building', u'modile', u'test']
sheet_number: 4
sheet_object: [<xlrd.sheet.Sheet object at 0x10244c190>, <xlrd.sheet.Sheet object at 0x10244c150>, <xlrd.sheet.Sheet object at 0x10244c110>, <xlrd.sheet.Sheet object at 0x10244c290>]
By_name: <xlrd.sheet.Sheet object at 0x10244c290>
By_index: <xlrd.sheet.Sheet object at 0x10244c290>

4、获取sheet的汇总数据:

获取sheet名:sheet1.name

获取总行数:sheet1.nrows

获取总列数:sheet1.ncols

# -*- coding:utf-8 -*-

import xlrd
import os
from datetime import date,datetime

filename = "demo.xlsx"
filePath = os.path.join(os.getcwd(), filename)
print filePath

# 打开文件
x1 = xlrd.open_workbook(filePath)

# 获取sheet的汇总数据
sheet1 = x1.sheet_by_name("plan")
print "sheet name:", sheet1.name   # get sheet name
print "row num:", sheet1.nrows  # get sheet all rows number
print "col num:", sheet1.ncols  # get sheet all columns number

输出:

sheet name: plan
row num: 31
col num: 11

资料:https://www.jb51.net/article/239873.htm

如何利用Python处理excel表格中的数据

https://www.jb51.net/article/187025.htm

如何利用Python处理excel表格中的数据

二、提高

三、出错

1.无法打开.xlsx文件 pandas无法打开.xlsx文件,xlrd.biffh.XLRDError: Excel xlsx file; not supported

安装的版本太高,低版本支持

可以安装旧版xlrd,在cmd中运行:

pip uninstall xlrd
pip install xlrd==1.2.0

也可以用openpyxl代替xlrd打开.xlsx文件:

df=pandas.read_excel(‘data.xlsx',engine=‘openpyxl')

来源:https://blog.csdn.net/sinat_37281674/article/details/123293158

标签:python,excel,数据
0
投稿

猜你喜欢

  • 在Django model中设置多个字段联合唯一约束的实例

    2021-02-09 22:04:59
  • Python如何实现大型数组运算(使用NumPy)

    2023-05-11 21:49:05
  • Pytorch如何把Tensor转化成图像可视化

    2021-11-03 20:20:22
  • Python 高效编程技巧分享

    2022-12-13 10:23:43
  • Python人工智能之路 jieba gensim 最好别分家之最简单的相似度实现

    2023-07-20 09:20:03
  • asp(JavaScript)自动判断网页编码并转换的代码

    2011-03-03 11:19:00
  • Python 人工智能老照片修复算法学习

    2022-10-31 03:37:18
  • Python实现将xml导入至excel

    2023-10-01 06:17:45
  • 一列保存多个ID(将多个用逗号隔开的ID转换成用逗号隔开的名称)

    2012-08-21 10:37:37
  • MySQL的远程连接出现错误提示分析

    2011-07-01 11:34:00
  • 发散后的期望

    2008-07-31 18:32:00
  • python numpy元素的区间查找方法

    2021-12-22 17:10:31
  • python 中字典嵌套列表的方法

    2022-05-17 04:54:15
  • Bootstrap实现响应式导航栏效果

    2023-08-13 15:32:13
  • python中使用np.delete()的实例方法

    2023-02-07 10:19:47
  • Python深拷贝与浅拷贝用法实例分析

    2023-11-06 01:25:04
  • ie和火狐兼容问题

    2010-07-02 12:50:00
  • python BlockingScheduler定时任务及其他方式的实现

    2022-02-13 17:32:25
  • Python numpy 模块介绍

    2022-06-04 02:03:00
  • Python math库 ln(x)运算的实现及原理

    2023-09-11 18:12:28
  • asp之家 网络编程 m.aspxhome.com