解决python读取excel日期格式问题(日期变为数字,int变为double)
生活随笔
收集整理的這篇文章主要介紹了
解决python读取excel日期格式问题(日期变为数字,int变为double)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
excel數(shù)據(jù)如下:
讀取excel
# 讀取excel文件 excel_data = xlrd.open_workbook(excel_path) # 獲取第一個sheet頁 sheet = excel_data.sheet_by_index(0) # 讀取數(shù)據(jù) for i in range(0, rows):for j in range(0, cols):print(sheet.cell(i,j).value)打印結(jié)果
20200302.0 18560726646.0 43888.0這里把整型數(shù)字和日期格式的數(shù)據(jù)都打印錯了,不是我們想要的結(jié)果
解決格式問題
# 解決整型和日期型的格式問題 def format_excel(i,j):# 表格的數(shù)據(jù)類型# ctype: 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 errorctype = sheet.cell(i, j).ctypecell = sheet.cell_value(i, j)if ctype == 2 and cell % 1 == 0: # 如果是整形cell = int(cell)elif ctype == 3:# 轉(zhuǎn)成datetime對象cell = xldate_as_datetime(cell, 0).strftime('%Y/%m/%d')return cell- 這樣,再讀取excel就格式就不會亂了
- 打印結(jié)果
總結(jié)
以上是生活随笔為你收集整理的解决python读取excel日期格式问题(日期变为数字,int变为double)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 志宇-dubbo源码分析
- 下一篇: 用python实现psnr