簡體   English   中英

根據時間段使用pandas讀取home目錄下的excel文件

[英]Read the excel file in the home directory using pandas based on the timeframe

我想在我的腳本中查看兩個文件之間的差異/差異。 但我在這里很震驚; 例如,我將使用 Flask 框架上傳文件。

文件編號 1 於下午 3:00 上傳

文件編號 2 於下午 2:50 上傳

import pandas as pd

df = pd.read_excel (r'Path where the Excel file is stored\File Number1.xlsx')
print (df)

根據時間范圍示例,我如何編寫代碼來讀取前一個文件,我想查看前一個文件,顯然是下午 2.50(文件編號 2)

df1 = pd.read_excel ()
print (df1)

您可以從該路徑獲取文件名並按修改時間對其進行排序並打印最后兩個文件。

from pathlib import Path

fnames = Path("Path/of/xlsx files").glob("*.xlsx")
fnames_with_modtime = [(x, x.stat().st_mtime) for x in fnames]
#sort by modification time

fnames_with_modtime.sort(key = lambda x: x[1])

#last modified two files
file1, _ = fnames_with_modtime[-1]
file2, _ = fnames_with_modtime[-2]

df1 = pd.read_excel(file1)
df2 = pd.read_excel(file2)
print(df1)
print(df2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM