找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2541|回复: 0

[求助] python读excel日期格式

5

主题

14

帖子

14

积分

贫民

积分
14
sally786 发表于 2018-1-10 21:55:32 | 显示全部楼层 |阅读模式
excel中有日期格式的单元格 用python读的时候 写了以下循环 但报错 不知道错在了哪里 请各位大神赐教

def FormatDatetimeValue(file= '.../xx.xlsx',by_name=u'xx'):  
    data = open_excel(file)
    table = data.sheet_by_name(by_name)
    nrows = table.nrows #行数
    ncols = table.ncols #列数
    date=[]
    for row in range(0,nrows):
        for col in range(0,ncols):
            cell = sheet.cell(row,col)
            if cell.ctype == 3: # 3 means 'xldate' , 1 means 'text  
                month, day,year = xlrd.xldate_as_tuple(cell.value,0)[:3]  
                py_date = datetime.date(month,day,year)  
                date.append(py_date.strftime('%m-%d-%Y'))

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表