python 读取excel 表格的数据
生活随笔
收集整理的這篇文章主要介紹了
python 读取excel 表格的数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
自己隨便寫了一個表格如下
使用工具xlrd 如果沒有下載 可以使用pip install xlrd 下載一個
大致需要一下幾個步驟
1 打開excel
table = xlrd.open_workbook("/home/hly/hly/test.xls")
2 獲取那一個excel 不如下面是獲取第二個excel
sheet = table.sheet_by_index(1)
3 獲取行數
rows = sheet.nrows
4 獲取每行的內容
row_values
具體代碼如下
# -*- coding: utf-8 -*-
import xlrd
import sys
reload(sys)
sys.setdefaultencoding("utf-8")table = xlrd.open_workbook("/home/hly/hly/test.xls")
# 通過索引獲取,例如打開第二個sheet表格
sheet = table.sheet_by_index(1)
# 獲取第二個excel的行數
rows = sheet.nrows
print rows
# 根據行數遍歷出整個表格
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)
打印結果如下
這里只是遍歷行數,當然方法不只是這一種。
總結
以上是生活随笔為你收集整理的python 读取excel 表格的数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求一个好听的将军名字
- 下一篇: python 往excel 里面写数据