zoukankan      html  css  js  c++  java
  • python 处理Excel 常见问题-读取Excel中时间,封装函数

     1 import xlrd
     2 from xlrd import xldate_as_tuple
     3 from datetime import datetime
     4 
     5 #打印Excel表格数据类型,寻找对应的结局函数
     6 def ensure_cell_type():
     7     book = xlrd.open_workbook('example.xlsx')
     8     sheet = book.sheet_by_name('Sheet1')
     9     for row in range(sheet.nrows):
    10         for col in range(sheet.ncols):
    11             print(sheet.cell(row,col))
    12             print('type',sheet.cell(row,col).ctype)
    13 
    14 #转换Excel中时间格式,此处的类型是3
    15 def tran_time_form():
    16     book = xlrd.open_workbook('example.xlsx')
    17     sheet = book.sheet_by_name('Sheet1')
    18     for row in range(sheet.nrows):
    19         for col in range(sheet.ncols):
    20             value = sheet.cell(row, col).value
    21             if sheet.cell(row, col).ctype == 3:
    22                 date = xldate_as_tuple(sheet.cell(row, col).value, 0)
    23                 print(date)
    24                 value = datetime(*date)
    25                 print(value)
    26 
    27 def main():
    28     ensure_cell_type()
    29     #tran_time_form()
    30 
    31 if __name__ == "__main__":
    32     main()
  • 相关阅读:
    「学习笔记」min_25筛
    HNOI2019游记
    【SDOI2017】数字表格
    【APIO2016】烟火表演
    【SCOI2015】小凸想跑步
    java Thread源码分析
    java ThreadGroup源码分析
    bean获取Spring容器
    spring 管理bean
    thinkphp5.0.19 request
  • 原文地址:https://www.cnblogs.com/dog-and-cat/p/11389983.html
Copyright © 2011-2022 走看看