1. 程式人生 > >python 讀取excel表格內不同型別的資料

python 讀取excel表格內不同型別的資料

不同型別資料對應值:
在這裡插入圖片描述

#coding=utf-8
import os
import xlrd
from datetime import datetime,date

newparh = os.chdir(r"D:\Pycharm\Test\test_case\excel")
filename = u'測試資料.xlsx'
file = os.path.join(os.getcwd(),filename)

xl = xlrd.open_workbook(file) #開啟檔案
table1 = xl.sheet_by_name(u"目錄")

def read_excel(table,
row,col): name = table.cell_value(row,col) type = table.cell_type(row,col) if type == 0: #空0 name = "''" elif type == 1: #字串1 name = name elif type == 2 and name%1==0: #數字2 name = int(name) elif type == 3: #日期3 date_value = xlrd.xldate_as_tuple(
name,0) name = datetime(*date_value).strftime('%Y/%m/%d %H:%M:%S') elif type == 4: #布林4 name = True if name == 1 else False elif type == 5: #error 5 name = error return name print(read_excel(table1,0,7))

轉自 碎片拾零

原文連結:http://www.cnblogs.com/feiyueNotes/p/7786739.html