从Python

时间:2017-02-24 03:34:45

标签: python excel

我有一张包含多张纸的xlsx文件。在表中有A列 带时间戳(作为字符串)。我需要从它上面的单元中减去单元格以查看已经过了多长时间。

离。 COLUMN A

02/23/2017 08:25:39
02/23/2017 08:55:56
02/23/2017 08:55:57
02/23/2017 08:56:12

这是我到目前为止所做的.....提前谢谢你。

import xlrd
from datetime import datetime

def open_file(path):

    # Open and read an Excel file

    book = xlrd.open_workbook(path)

    # get the first worksheet

    first_sheet = book.sheet_by_index(0)

    # read first column

    column_values = first_sheet.col_values(0,0)
    column_list = []

    for i in column_values:

        i = datetime.strptime(i, '%m/%d/%Y %H:%M:%S')

        column_list.append(i)

    print(column_list[1] - column_list[0])

if __name__ == "__main__":
    path = '02-23-2017.xlsx'
    open_file(path)

1 个答案:

答案 0 :(得分:0)

您可能想要查看大熊猫。它可以快速处理这样的计算。

import pandas as pd

# create a dictionary of data frames, one for each sheet
df_dict = pd.read_excel('C:/path/to/file.xlsx', sheets=None, header=None)

# iterate over each data frame
for df_key in df_dict:
    # pull the time data from the first columns
    t = pd.to_datetime(df_dict[df_key].iloc[:,0])
    # calculate the time difference using .diff(1), fillna makes the first cell 0
    dt = t.diff(1).fillna(0)
    # assign the difference to a new columns in the data frame
    df_dict[df_key]['time_delta'] = dt

# create a writer to make a new excel file
writer = pd.ExcelWriter('C:/path/to/new_file.xlsx')
# write each sheet to file
for name, df in df_dict.items():
    df.to_excel(writer, 'sheet{}'.format(name))
writer.save()