简体   繁体   中英

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

I want to see the difference/variance between two files in my script. but i am strucking at here; for example i will upload the file using flask framework.

File Number 1 uploaded at 3:00 PM

File Number 2 uploaded at 2:50 PM

import pandas as pd

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

how do I write the code to read the previousfile accroding to the timeframe example I want to see the previousfile, obviously 2.50 PM (File Number2)

df1 = pd.read_excel ()
print (df1)

You can get the file names from that path and sort it by modification time and print the last two files.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM