自動(dòng)整理時(shí)間記錄的代碼

用于每天記錄時(shí)間和事件,用程序自動(dòng)統(tǒng)計(jì),用透視表自動(dòng)分析。

1.記錄時(shí)間

記錄的格式為
“小時(shí):分鐘——小時(shí):分鐘,時(shí)間”
舉例

22:48——6:58 夜睡。
8:09——8:19 聽英語。

記錄軟件是flomo。

2.統(tǒng)計(jì)時(shí)間

這個(gè)代碼是對(duì)記錄進(jìn)行統(tǒng)計(jì),輸出結(jié)果為:開始時(shí)間、結(jié)束時(shí)間、時(shí)長、事件。
輸入是直接復(fù)制上面的時(shí)間記錄,然后運(yùn)行程序。我用的是PyCharm
Community Edition 2024.1.4。
輸出形式是excel表格的數(shù)據(jù)。

#!python
# 用于整理每天的時(shí)間記錄。

import pyperclip
import pandas as pd
from datetime import timedelta
import os

text = pyperclip.paste()

def statistical_time_records(text):
    # Split the text into lines
    lines = text.split('\n\n')
    print(lines)

    # Prepare lists to hold the data
    start_times = []
    end_times = []
    events = []

    # Process each line
    for line in lines:
        # Split the line into time part and event part using the last occurrence of ','
        if ' ' in line:
            time_part, event_part = line.rsplit(' ')
        else:
            continue  # Skip lines that don't contain an event part

        # Split the time part into start and end times
        start_time, end_time = time_part.split('——')

        # Clean up the strings
        start_time = start_time.strip()
        end_time = end_time.strip()
        event = event_part.strip()

        # Append to lists
        start_times.append(start_time)
        end_times.append(end_time)
        events.append(event)

    # Create a DataFrame
    df = pd.DataFrame({
        'Start Time': start_times,
        'End Time': end_times,
        'Event': events
    })

    # Convert Start Time and End Time to timedelta
    df['Start Time'] = pd.to_timedelta(df['Start Time'].apply(lambda x: f"00:{x}"))
    df['End Time'] = pd.to_timedelta(df['End Time'].apply(lambda x: f"00:{x}"))

    # Calculate Duration
    def calculate_duration(row):
        if row['End Time'] < row['Start Time']:
            # Calculate the duration using 24:00
            duration = (timedelta(minutes=24) - row['Start Time']) + row['End Time']
        else:
            duration = row['End Time'] - row['Start Time']
        return duration

    df['Duration'] = df.apply(calculate_duration, axis=1)

    # Format Start Time, End Time, and Duration to minute:second
    df['Start Time'] = df['Start Time'].dt.components.apply(lambda x: f"{int(x['minutes'])}:{int(x['seconds']):02}",
                                                            axis=1)
    df['End Time'] = df['End Time'].dt.components.apply(lambda x: f"{int(x['minutes'])}:{int(x['seconds']):02}", axis=1)
    df['Duration'] = df['Duration'].dt.components.apply(lambda x: f"{int(x['minutes'])}:{int(x['seconds']):02}", axis=1)

    df = df[['Start Time', 'End Time', 'Duration', 'Event']]


    # Print the string representation of the DataFrame
    df.to_excel('remote_put.xlsx', index=False)
    # Open the Excel file

    os.system('start excel remote_put.xlsx')

    return df


statistical_time_records(text)

3.分析時(shí)間

將數(shù)據(jù)放到excell里的時(shí)間記錄表里,事件后面加上分類列,將事件根據(jù)自己的需要分類,比如“睡眠”、“工作”、“閱讀”和“寫作”等。

點(diǎn)擊【插入】——【透視表】,即可分析一天的時(shí)間情況。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容