导入Excel数据并保留日期时间

时间:2016-09-27 08:46:06

标签: excel python-2.7

提前感谢您的帮助。我使用openpyxl从excel导入数据虽然我希望将字符串输入到datetime,但下面是我使用的代码:

import openpyxl, pprint, datetime
print ('Opening workbook...')
wb= openpyxl.load_workbook('ACLogs_test_Conv2.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
print sheet
ACLogsData = {}
print ('Reading rows...')
for row in range(2, sheet.max_row +1):
    pangalan  = sheet['B' + str(row)].value
    dates = sheet['D' + str(row)].value
    time = sheet['E' + str(row)].value
    ACLogsData.setdefault(pangalan,{})
    ACLogsData[pangalan].setdefault(dates,{})
    ACLogsData[pangalan][dates].setdefault(time)

1 个答案:

答案 0 :(得分:0)

使用datetime.strptime()

FMT = '%H:%M' # Whatever format your times are in

for row in range(2, sheet.max_row +1):
    pangalan  = sheet['B' + str(row)].value
    dates = sheet['D' + str(row)].value
    time = datetime.strptime(sheet['E' + str(row)].value, FMT)