日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Python xlrd 读取excel表格 常用用法整理

發布時間:2023/11/27 生活经验 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python xlrd 读取excel表格 常用用法整理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

xlrd 的使用?

#!/usr/bin/python#
# -*- coding: utf-8 -*-
import xlrd
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
# 打開excel
table = xlrd.open_workbook('/home/hly/hly/test.xls')
# excel 地步表格的名稱
sheetName = table.sheet_names()
print(sheetName)
# 獲取sheet對象
print (table.sheets())
# 獲取sheet總數
print(table.nsheets)
# 通過索引獲取,例如打開第二個sheet表格
sheet = table.sheet_by_index(1)
print (sheet)
# 通過sheet名稱獲取,例如打開第二個sheet表格
# sheet = table.sheet_by_name("Sheet")
# print (sheet)
# 獲取名稱
print sheet.name
# 獲取行數
rows = sheet.nrows
print (rows)# 獲取列數
cols = sheet.ncols
print (cols)# 獲取第一行的數據
one_content = str(sheet.row_values(0)).replace('u\'', '\'').decode("unicode-escape")
print (one_content)
# 獲取第一行表格的內容以及內容的類型
one_content = str(sheet.row(0)).replace('u\'', '\'').decode("unicode-escape")
print (one_content)# 獲取第一列的數據
one_col_content = str(sheet.col_values(0)).replace('u\'', '\'').decode("unicode-escape")
print one_col_content# 單元格的類型
table_content = sheet.cell_type(2,2)
print (table_content)# 單元格的內容和類型
table_content = sheet.cell(2, 2)
print (table_content)# 單元格的內容
table_content = sheet.cell_value(2, 2)
print (table_content)# 根據行數遍歷出整個表格
content_list = []
for i in range(rows):content_list.append(sheet.row_values(i))
# 處理list中文亂碼
case_list = str(content_list).replace('u\'', '\'').decode("unicode-escape")
print (case_list)

xlrd 的注意事項

當使用xlrd 讀取excle 表格的時候需要注意xlrd 的版本

xlrd 版本低于或者等于1.2.0 的時候 讀取excel 不支持中文名稱的excel 但是確實 以.xls 和 .xlsx 結尾的 excel

xlrd 版本是最新版本的時候目前最新的版本為2.0.1 支持中為名稱的excel 但是 不支持.xlsx 結尾的excel ,只支持.xls結尾的excel

?

總結

以上是生活随笔為你收集整理的Python xlrd 读取excel表格 常用用法整理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。