我的代碼 -
for file in os.listdir("C:/Users/hhh/Desktop/autotranscribe/python/Matching"):
if file.startswith("Master"):
dfs = pd.read_excel(file, sheet_name=None)
output = dict()
for ws, df in dfs.items():
if ws in ["Added"]:
continue
if ws in ["All Members", "Terms"]:
temp = df
>temp
Company State Address Zip City
ABC CA 1 st 86284 LA
我想要另一串列示檔案在目錄中創建/洗掉的月份減去 1(檔案日期減去 1 個月)。所以基本上,如果檔案是在 22 年 5 月 5 日創建的。我想要下面的
Company State Address Zip City File Month
ABC CA 1 st 86284 LA 04-2022
我的嘗試——
import datetime
path = "C:/Users/hhh/Desktop/autotranscribe/python/Matching"
# file creation timestamp in float
c_time = os.path.getctime(path)
# convert creation timestamp into DateTime object
dt_c = datetime.datetime.fromtimestamp(c_time)
temp['File Month'] = (dt_c.replace(day=1) - datetime.timedelta(days=1)).strftime("%m-%Y")
但這僅在我設定路徑而不是在我的情況下才有效,因為您在上面看到的路徑基于以“Master”開頭的檔案名。我只希望它基于檔案名而不是 os 路徑,因為 os 路徑每天都會在其中創建多個檔案。我該怎么做呢?
uj5u.com熱心網友回復:
將用于更改時間的代碼移動到for
回圈中。此外,您不需要datetime
. pandas
擁有您需要的所有操作。
path = "C:/Users/hhh/Desktop/autotranscribe/python/Matching"
for file in os.listdir(path):
if file.startswith("Master"):
dfs = pd.read_excel(file, sheet_name=None)
output = dict()
for ws, df in dfs.items():
if ws in ["Added"]:
continue
if ws in ["All Members", "Terms"]:
temp = df
dt = pd.to_datetime(os.path.getctime(os.path.join(path,file)),unit="s").replace(nanosecond=0)
temp['File Month'] = (dt.replace(day=1)-pd.DateOffset(days=1)).strftime("%m-%Y")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/472618.html
上一篇:熊貓-在n個月前取值
下一篇:以特定方式拆分日期范圍的問題