使用python xlrd将日期作为字符串而不是从excel中浮动 [英] Reading date as a string not float from excel using python xlrd

查看:104
本文介绍了使用python xlrd将日期作为字符串而不是从excel中浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在 Excel 格式中读取日期蟒蛇?

我的日期可以在 excel 文件中的任何字段中,但是当我使用 python xlrd 读取它时,它被读取为浮点数.有没有办法将所有excel单元格读取为字符串?

My date can be among any field in an excel file but when I read it using python xlrd its being read as a float. Is there a way to read all the excel cells as string?

我想准备一个脚本来生成一个文件,其中包含由管道分隔的 excel 文件中的所有值,但这个日期问题正在产生问题.

I want to prepare a script to generate a file having all the values in excel file separated by a pipe but this date thing is creating problem.

推荐答案

Excel 将日期存储为浮点数.如果您想转换它们,xlrd 有一个功能可以帮助您:xldate_as_tuple

Excel stores dates as floats. If you want to convert them xlrd has a function to help you with this: xldate_as_tuple

一个例子:

import datetime, xlrd
book = xlrd.open_workbook("myfile.xls")
sh = book.sheet_by_index(0)
a1 = sh.cell_value(rowx=0, colx=0)
a1_as_datetime = datetime.datetime(*xlrd.xldate_as_tuple(a1, book.datemode))
print 'datetime: %s' % a1_as_datetime

这篇关于使用python xlrd将日期作为字符串而不是从excel中浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆