Python xldr 读取xls 文件中时间格式处理
前言:個人測試正在入門,今天試著測試去哪兒網(wǎng)站,自己寫了一個xls的文件用于存儲出發(fā)地、目的地、出發(fā)時間、返回時間。在讀取到返回時間時,在Pycharm運行輸出是日期格式成了一個浮點數(shù),我開始以為是讀取成了格林尼治時間,預(yù)估是時間間隔。為了驗證我試著計算1970年1月1日到出發(fā)時間之間的時間間隔,并不符合時間差;太扎心了老鐵,這怎么換算咯?幾番查閱資料,發(fā)現(xiàn)了解決方法,特做記錄。
----解決方式,可以直接看第9點----
1、先給您看下我xls文件中的數(shù)據(jù):
def read_excel(filename, index, ishead=False):xls = xlrd.open_workbook(filename)sheet = xls.sheet_by_index(index)print("date.today", date.today())data = []for i in range(sheet.nrows):print("----", sheet.row_values(i))if i == 0:if ishead:continuedata.append(sheet.row_values(i))return datadata = read_excel("qunaer.xls", 0, True)2、看運行輸出結(jié)果:
3、現(xiàn)在我們來計算一下這兩個日期相差的時間間隔,以第一條為例:
4、現(xiàn)在來判斷表格中數(shù)據(jù)的數(shù)據(jù)類型
def read_excel(filename, index, ishead=False):xls = xlrd.open_workbook(filename)sheet = xls.sheet_by_index(index)print("date.today", date.today())# 判斷數(shù)據(jù)類型for row in range(sheet.nrows):for col in range(sheet.ncols):print('--', sheet.cell(row, col), '--type:', sheet.cell(row, col).ctype)data = []for i in range(sheet.nrows):print("----", sheet.row_values(i))if i == 0:if ishead:continuedata.append(sheet.row_values(i))return datadata = read_excel("qunaer.xls", 0, True)?5、看運行輸出結(jié)果(只截取了部分,可對比xls表格中前四行數(shù)據(jù))
?
?6、那么輸出的type對應(yīng)的數(shù)據(jù)類型是什么呢?我們到 第44行代碼中調(diào)用的sheet.cell()中尋找答案-->查看源碼
# xlrd.sheet.Cellclass Cell(BaseObject):"""Contains the data for one cell... warning::You don't call this class yourself. You access :class:`Cell` objectsvia methods of the :class:`Sheet` object(s) that you found in the:class:`~xlrd.book.Book` object that was returned when you called:func:`~xlrd.open_workbook`Cell objects have three attributes: ``ctype`` is an int, ``value``(which depends on ``ctype``) and ``xf_index``.If ``formatting_info`` is not enabled when the workbook is opened,``xf_index`` will be ``None``.The following table describes the types of cells and how their valuesare represented in Python... raw:: html<table border="1" cellpadding="7"><tr><th>Type symbol</th><th>Type number</th><th>Python value</th></tr><tr><td>XL_CELL_EMPTY</td><td align="center">0</td><td>empty string ''</td></tr><tr><td>XL_CELL_TEXT</td><td align="center">1</td><td>a Unicode string</td></tr><tr><td>XL_CELL_NUMBER</td><td align="center">2</td><td>float</td></tr><tr><td>XL_CELL_DATE</td><td align="center">3</td><td>float</td></tr><tr><td>XL_CELL_BOOLEAN</td><td align="center">4</td><td>int; 1 means TRUE, 0 means FALSE</td></tr><tr><td>XL_CELL_ERROR</td><td align="center">5</td><td>int representing internal Excel codes; for a text representation,refer to the supplied dictionary error_text_from_code</td></tr><tr><td>XL_CELL_BLANK</td><td align="center">6</td><td>empty string ''. Note: this type will appear only whenopen_workbook(..., formatting_info=True) is used.</td></tr></table>"""__slots__ = ['ctype', 'value', 'xf_index']def __init__(self, ctype, value, xf_index=None):self.ctype = ctypeself.value = valueself.xf_index = xf_indexdef __repr__(self):if self.xf_index is None:return "%s:%r" % (ctype_text[self.ctype], self.value)else:return "%s:%r (XF:%r)" % (ctype_text[self.ctype], self.value, self.xf_index)7、分析部分源碼,查看提示
?8、總結(jié)一下我們xls表中出現(xiàn)的數(shù)據(jù)類型
| 類型碼 | 數(shù)據(jù)類型 | Python值 |
| 1 | Text | String |
| 2 | Number | float |
| 3 | Date | float |
也就是說在xls表格中的數(shù)據(jù)在Python中用xldr讀取后,數(shù)據(jù)類型不完全一致
9、解決方式
import datetime from xlrd import xldate_as_tupledef read_excel(filename, index, ishead=False):xls = xlrd.open_workbook(filename)sheet = xls.sheet_by_index(index)print("date.today", date.today())# 判斷數(shù)據(jù)類型# for row in range(sheet.nrows):# for col in range(sheet.ncols):# print('--', sheet.cell(row, col), '--type:', sheet.cell(row, col).ctype)data = []for i in range(sheet.nrows):data_list_i = []if i == 0:if ishead:continueif i > 0: # 從第2行 第3列開始的date日期類型數(shù)據(jù),需要修改的日期只在第3列(2=3-1)start_Time_float = sheet.cell(i, 2)# 輸出獲取到的從第二行開始的每個第三列數(shù)據(jù)的 數(shù)據(jù)類型 及 數(shù)據(jù) 及 類型碼# print("==========", type(start_Time_float), start_Time_float, start_Time_float.ctype)# 把獲取從第二行開始的每個第三列數(shù)據(jù)轉(zhuǎn)成我們習(xí)慣的日期格式,需要 import datetime 以及 from xlrd import xldate_as_tuplestart_Time = datetime.datetime(*(xldate_as_tuple(start_Time_float.value, 0)))# print("__~~__", start_Time_float.value, type(start_Time_float.value), type(start_Time), start_Time)data_list_i = sheet.row_values(i)data_list_i[2] = start_Timeprint("---", i, data_list_i[2], data_list_i)data.append(data_list_i)return datadata = read_excel("qunaer.xls", 0, True)10、看運行結(jié)果
?
11、如果要判斷每個單元格的數(shù)據(jù)類型,修改為日期類型--代碼示例:
nrows = sheet.nrowsncols = sheet.ncolsfor row in range(1, nrows):for col in range(ncols):date_float = sheet.cell(row, col).valueif sheet.cell(row, col).ctype == 3:date_time = datetime.datetime(*xldate_as_tuple(date_float, 0))print(date_time)12、僅做記錄,感謝您的參考,如果您有建議,不勝感激
總結(jié)
以上是生活随笔為你收集整理的Python xldr 读取xls 文件中时间格式处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python中7种随机函数总结
- 下一篇: Python语言的适用范围