我有一个excel文件,它有3列作为日期时间或日期或时间字段。我是通过xlrd包来阅读的,我得到的时间是milliseconds我想,当我试图把它转换回datetime时,我得到了错误的结果。在
我也尝试将文件转换为csv。这也没用,我得到了奇怪的日期时间格式,我无法理解。在
下面是我尝试的xlrd格式。我更喜欢使用扩展名为.xlrs的文件作为输入,否则每次获得新的输入文件时,我都必须将excel文件转换为.csv。在from xlrd import open_workbook
import os,pickle,datetime
def main(path, filename, absolute_path_organisation_structure):
absolute_filepath = os.path.join(path,filename)
wb = open_workbook(absolute_filepath)
for sheet in wb.sheets():
number_of_rows = sheet.nrows
number_of_columns = sheet.ncols
for row_index in xrange(1, sheet.nrows):
row=[]
for col_index in xrange(4,7): #4th and 6th columns are date fields
row.append(sheet.cell(row_index, col_index).value)
print(row) #Relevant list formed with 4th, 5th and 6th columns
print(datetime.datetime.fromtimestamp(float(row[0])).strftime('%Y-%m-%d %H:%M:%S'))
path = "C:\\Users\\***************\\NEW DATA"
MISfile = "P2P_2015 - Copy.xlsx"
absolute_path_organisation_structure = "C:\\Users\\******************NEW DATA\\organisation.csv"
main(path, MISfile, absolute_path_organisation_structure)
结果:
^{pr2}$
实际输入文件:(从excel复制)1/7/2015 11:13 Registered 1/5/2015 0:00
1/7/2015 11:13 Sent for CTG1 approval 1/6/2015 0:00
1/7/2015 11:13 Sent back 1/6/2015 0:00
1/7/2015 11:13 Registered 1/7/2015 0:00
1/7/2015 11:13 Sent for CTG1 approval 1/7/2015 0:00
1/7/2015 11:13 Sent for CTG2 approval 1/8/2015 0:00
1/7/2015 11:13 CTG2 Approved 1/8/2015 0:00
1/7/2015 11:13 Sent back 1/9/2015 0:00
6/15/2015 14:48 Registered 5/20/2015 0:00
6/15/2015 14:48 Registered 5/20/2015 0:00
6/15/2015 14:48 Sent back 6/10/2015 0:00
6/15/2015 14:48 Sent back 6/10/2015 0:00
6/15/2015 14:48 Registered 6/15/2015 0:00
6/15/2015 14:48 Registered 6/15/2015 0:00
为什么我不能正确地读出日期?为什么它们不简单地变成字符串,这样我就可以很容易地转换它们?在