我有一個 python 腳本,我想從特定檔案夾呼叫 csv 和 xlsx,而不必指定運行程式所需的每個檔案的目錄。
import pandas as pd
df = pd.read_excel("WindDirectionWest2.xlsx")
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
hey=df["WindDirection"].mean()
print(int(hey))
df = pd.read_excel("WindDirectionSouth2.xlsx")
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
he=df["WindDirection"].mean()
print(int(he))
df = pd.read_excel("WindDirectionEast2.xlsx")
df.drop(df.columns[df.columns.str.contains('unnamed',case = False)],axis = 1, inplace = True)
he=df["WindDirection"].mean()
print(int(he))
所以,我想在我的腳本頂部有一個代碼來呼叫一個檔案夾,然后將每個檔案路徑寫入每個代碼變得多余。
Ps我不知道如何將代碼作為python。我只能看到 Javascript、CSS 和 HTML。
uj5u.com熱心網友回復:
如果我理解得很好:
import os
import pandas as pd
path = '/some/path/to/file'
for file in os.listdir(path):
df = pd.read_excel(os.path.join(path, file))
uj5u.com熱心網友回復:
如果你想這樣做,你可以使用“pkg_resources”模塊!您的資料檔案需要在您的包內。
import pkg_resources
pkg_resources.resource_filename('packageName', 'path/of/file/in/my/package.xlsx')
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376578.html
