Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将xldate转换为python日期时间_Python_Python 2.7 - Fatal编程技术网

将xldate转换为python日期时间

将xldate转换为python日期时间,python,python-2.7,Python,Python 2.7,使用以下代码将excel xldate转换为python datetime时遇到问题。这是转换的正确方式吗 import xlrd from datetime import datetime book = xlrd.open_workbook("a.xls") sh = book.sheet_by_index(0) for rx in range(1,sh.nrows): a = sh.row(rx) print a year, month, day, hour,

使用以下代码将excel xldate转换为python datetime时遇到问题。这是转换的正确方式吗

import xlrd
from datetime import datetime

book = xlrd.open_workbook("a.xls")
sh = book.sheet_by_index(0)
for rx in range(1,sh.nrows):
    a = sh.row(rx)
    print a  
    year, month, day, hour, minute = xlrd.xldate_as_tuple(a[0], book.datemode)
    py_date = datetime.datetime(year, month, day, hour, minute)

a已打印-->


下面显示了错误

  year, month, day, hour, minute = xlrd.xldate_as_tuple(a[0], book.datemode)

  File "C:\Anaconda\Lib\site-packages\xlrd\xldate.py", line 67, in xldate_as_tuple
    xldays = int(xldate)
  TypeError: int() argument must be a string or a number, not 'Cell'
a[0]
是a,因此需要使用
a[0].value
来访问单元格内容

此外,您不能解包到
年、月、日、小时、分钟
,因为生成的元组中有五个以上的值(关于
?),但是,您可以通过使用元组解包(参见示例)而不是临时变量来避免:

py_date = datetime.datetime(*xlrd.xldate_as_tuple(a[0].value,
                                                  book.datemode))

@jonrsharpe的公认答案很有效。但这更干净:

py_date = xlrd.xldate.xldate_as_datetime(a[0].value, book.datemode)

这不应该是
a[0].value
?value错误:太多的值无法解压,因此无法给出问题的答案;如果你同时想清楚了,你可以回答自己的问题。
py_date = xlrd.xldate.xldate_as_datetime(a[0].value, book.datemode)