簡體   English   中英

python / pandas - 從目錄導入 last modified.xlsx 文件

[英]python / pandas - Import last modified .xlsx file from a directory

我在 Dropbox 中有一個帶有路徑的文件夾:

C:\Users\Sophocles PC\Dropbox\RGS Delivery\Impact Coefficients\Consumer Discretionary\CD_LG_Apparel_Accessories_Luxury_Goods\Data\CD_LG_DataOutputs

我想導入最后修改的.xlsx文件。 我使用代碼:

list_of_files = glob.glob(r'C:\Users\Sophocles PC\Dropbox\RGS Delivery\Impact Coefficients\Consumer Discretionary\CD_LG_Apparel_Accessories_Luxury_Goods\Data\CD_LG_DataOutputs/*') 
latest_file = max(list_of_files, key=os.path.getctime)

latest_file ,打印:

C:\Users\Sophocles PC\Dropbox\RGS Delivery\Impact Coefficients\Consumer Discretionary\CD_LG_Apparel_Accessories_Luxury_Goods\Data\CD_LG_DataOutputs\~$CD_LG_Results - 2020-11-27 - mean - v2.xlsx

這實際上是最新的文件,因此是我要導入的文件。 但是,當我嘗試導入文件時:

x = pd.read_excel(latest_file)

我收到以下錯誤: XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\x0cSophocl' XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\x0cSophocl'

我不知道如何解決這個錯誤,但我也認為解決這個問題應該是相當直接的。 對此的任何幫助將不勝感激。 謝謝你。

使用以下代碼刪除~$ (臨時文件)后問題得到解決:

x = pd.read_excel(f"{latest_file}".replace('~$',''),sheet_name=2)

謝謝。

暫無
暫無

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

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