日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

如何利用Python處理excel表格中的數據_python

作者:行走的算法 ? 更新時間: 2022-05-06 編程語言

一、基礎、常用方法

1. 讀取excel

1、導入模塊:

import xlrd

2、打開文件:

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

3、獲取sheet:

sheet是指工作表的名稱,因為一個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: [, , , ]
By_name: 
By_index: 

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

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

二、提高

三、出錯

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

欄目分類
最近更新