使用python xlrd将日期作为字符串而不是从excel中浮动

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13962837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:02:43  来源:igfitidea点击:

Reading date as a string not float from excel using python xlrd

pythonxlrd

提问by Kundan Kumar

Possible Duplicate:
How do I read a date in Excel format in Python?

可能的重复:
如何在 Python 中读取 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 文件中的任何字段中,但是当我使用 python xlrd 读取它时,它被读取为浮点数。有没有办法将所有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 文件中的所有值,但是这个日期问题正在产生问题。

采纳答案by jojo

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

Excel 将日期存储为浮点数。如果你想转换它们xlrd有一个函数来帮助你:xldate_as_tuple

An exmple:

一个例子:

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