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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python读取excel,数字都是浮点型,日期格式是数字的解决办法

發布時間:2023/12/18 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python读取excel,数字都是浮点型,日期格式是数字的解决办法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

excel文件內容:

?

讀取excel:

# coding=utf-8 import xlrd import sysreload(sys) sys.setdefaultencoding('utf-8') import tracebackclass excelHandle:def decode(self, filename, sheetname):try:filename = filename.decode('utf-8')sheetname = sheetname.decode('utf-8')except Exception:print traceback.print_exc()return filename, sheetnamedef read_excel(self, filename, sheetname):filename, sheetname = self.decode(filename, sheetname)rbook = xlrd.open_workbook(filename)sheet = rbook.sheet_by_name(sheetname)rows = sheet.nrowscols = sheet.ncolsall_content = []for i in range(rows):row_content = []for j in range(cols):cell = sheet.cell_value(i, j)row_content.append(cell)all_content.append(row_content)print '[' + ','.join("'" + str(element) + "'" for element in row_content) + ']'return all_contentif __name__ == '__main__':eh = excelHandle()filename = r'G:\test\ctype.xls'sheetname = 'Sheet1'eh.read_excel(filename, sheetname)

輸出:

['整形','175.0'] ['字符串','最后的騎士'] ['浮點型','6.23'] ['日期','42909.6461574'] ['空值',''] ['布爾型','1']

可以看到,數字一律按浮點型輸出,日期卻輸出成一串小數?!布爾型輸出0或1

?

代碼稍做改動:來看一看表格的數據類型

for i in range(rows):row_content = []for j in range(cols):ctype = sheet.cell(i, j).ctype #表格的數據類型print ctype,cell = sheet.cell_value(i, j)row_content.append(cell)all_content.append(row_content)printprint '[' + ','.join("'" + str(element) + "'" for element in row_content) + ']'

輸出:

1 2 ['整形','175.0'] 1 1 ['字符串','最后的騎士'] 1 2 ['浮點型','6.23'] 1 3 ['日期','42909.6461574'] 1 0 ['空值',''] 1 4 ['布爾型','1']

python讀取excel中單元格的內容返回的有5種類型,即上面例子中的ctype:


ctype: 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error

?

所以,判斷一下ctype,然后再做相應處理就可以了。

?

最終代碼:

# coding=utf-8 import xlrd import sysreload(sys) sys.setdefaultencoding('utf-8') import traceback from datetime import datetime from xlrd import xldate_as_tupleclass excelHandle:def decode(self, filename, sheetname):try:filename = filename.decode('utf-8')sheetname = sheetname.decode('utf-8')except Exception:print traceback.print_exc()return filename, sheetnamedef read_excel(self, filename, sheetname):filename, sheetname = self.decode(filename, sheetname)rbook = xlrd.open_workbook(filename)sheet = rbook.sheet_by_name(sheetname)rows = sheet.nrowscols = sheet.ncolsall_content = []for i in range(rows):row_content = []for j in range(cols):ctype = sheet.cell(i, j).ctype # 表格的數據類型cell = sheet.cell_value(i, j)if ctype == 2 and cell % 1 == 0: # 如果是整形cell = int(cell)elif ctype == 3:# 轉成datetime對象date = datetime(*xldate_as_tuple(cell, 0))cell = date.strftime('%Y/%d/%m %H:%M:%S')elif ctype == 4:cell = True if cell == 1 else Falserow_content.append(cell)all_content.append(row_content)print '[' + ','.join("'" + str(element) + "'" for element in row_content) + ']'return all_contentif __name__ == '__main__':eh = excelHandle()filename = r'G:\test\ctype.xls'sheetname = 'Sheet1'eh.read_excel(filename, sheetname)

輸出:

['整形','175'] ['字符串','最后的騎士'] ['浮點型','6.23'] ['日期','2017/23/06 15:30:28'] ['空值',''] ['布爾型','True']

轉自:https://www.cnblogs.com/xxiong1031/p/7069006.html

轉載于:https://www.cnblogs.com/liuyanhang/p/11078479.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python读取excel,数字都是浮点型,日期格式是数字的解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。

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