如果文件的最后修改日期是今天,我如何將Excel文件導入熊貓?

[英]How do I import an Excel file to Pandas if the file's last modified date is today?


So I'm trying to write a Python script that imports an excel file into a Pandas dataframe (condition being if the file was modified on a certain date, ex. today):

因此,我正在嘗試編寫一個Python腳本,該腳本將一個excel文件導入一個熊貓dataframe(條件是如果該文件在某個日期被修改,例如今天):

import pandas as pd
import glob
import os
import datetime

def report():
    for x in glob.glob("../*.xlsx"):
        modx = os.path.getmtime(x)
        xmod = datetime.datetime.fromtimestamp(modx)
    if datetime.datetime.today() == xmod:
        return x

I've considered importing the excel file right from the function:

我考慮過從函數中導入excel文件:

if datetime.datetime.today() == xmod:
    df = pd.read_excel(x)
    return df

An attempt to modify the dataframe (after attempting to import) yields this:

嘗試修改dataframe(嘗試導入后)會得到以下結果:

  File "<ipython-input-56-e6fa18118137>", line 1, in <module>
df = pd.read_excel(report())

  File "..\excel.py", line 151, in read_excel
return ExcelFile(io, engine=engine).parse(sheetname=sheetname, **kwds)

  File "..\excel.py", line 196, in __init__
raise ValueError('Must explicitly set engine if not passing in'

ValueError: Must explicitly set engine if not passing in buffer or path for io.

Couldn't dig up much on that. I'm not even sure if I need to define a function.

對那件事不了解。我甚至不確定是否需要定義一個函數。

  • Do I import into a Pandas DataFrame directly from the function? Or keep it separate?
  • 是否直接從函數中導入熊貓數據?還是保持獨立?
  • How would I go about setting the engine?
  • 我該怎樣去啟動引擎呢?

3 个解决方案

#1


0  

First off, use datetime.today().date(). This will ensure the correct comparison format with xmod.

首先,使用datetime.today().date()。這將確保與xmod的正確比較格式。

Second, that glob loop is kind of confusing. Are you sure you want the for and if to be in the same indentation level? Or should the if be inside the for loop? Either way, that needs to be fixed.

第二,glob循環有點混亂。您確定您希望for和if處於相同的縮進級別嗎?或者if應該在for循環中?無論哪種方式,都需要修正。

Also, try not passing in a function. Explicitly pass either a path or a buffer, as the error implies. Otherwise, it will ask you which engine to use, which is more work than it's worth for what you're doing.

另外,不要傳遞函數。如錯誤所示,顯式地傳遞路徑或緩沖區。否則,它會問你用哪個引擎,哪個引擎的工作量比你做的工作的價值要大。

All else, just subscribe to best programming/Python practices.

除此之外,只需訂閱最佳編程/Python實踐。

In [1]: import pandas as pd
    ...: from datetime import datetime
    ...: import os

In [2]: def load_if_modified_today(xls):
   ...:     modx = os.path.getmtime(xls)
   ...:     xmod = datetime.fromtimestamp(modx)
   ...:     if datetime.today().date() == xmod.date():
   ...:         df = pd.read_excel(xls)
   ...:         return df

In [3]: e = 'trades.xlsx'
   ...: df = load_if_modified_today(e)
   ...: df.head()
Out[3]: 
           System Name  Symbol  Unit Position Entry Date Entry Time  \
0  TB Turtle FX Majors  EURUSD     1    Short 2014-12-02        Day   
1  TB Turtle FX Majors  EURUSD     1    Short 2014-12-05        Day   
2  TB Turtle FX Majors  EURUSD     1     Long 2014-12-10        Day   
3  TB Turtle FX Majors  EURUSD     1     Long 2014-12-16        Day   
4  TB Turtle FX Majors  EURUSD     1    Short 2014-12-17        Day   

   Entry Order Price  Stop Price   Risk%  Quantity  Entry Fill  
0             1.2420      1.2428  0.0024         6      1.2418  
1             1.2284      1.2297  0.0022         4      1.2283  
2             1.2447      1.2434  0.0017         3      1.2448  
3             1.2496      1.2486  0.0026         5      1.2499  
4             1.2385      1.2397  0.0022         4      1.2383  

#2


0  

I came across same ValueError: Must explicitly set engine if not passing in buffer or path for io. After I altered my code to:

我遇到了相同的ValueError:如果沒有為io傳入緩沖區或路徑,則必須顯式設置引擎。在我將代碼更改為:

data = pd.read_excel(R"D:\\file.xlsx")

It worked.

它工作。

#3


-1  

I ran across a similar issue, and it turned out that different python frame versions installed were conflicting each other. I removed one of them (anaconda), kept and updated plain python2.7 and all was fixed.

我遇到了一個類似的問題,結果發現安裝的不同python框架版本相互沖突。我去掉了其中一個(蟒蛇),保留並更新了plain python2.7,所有的都被修復了。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/07/07/720c3a3abf73806d8197302dc548535b.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com