使用Python自动化文件读取和函数应用

时间:2018-05-21 07:20:25

标签: python pandas csv input trading

如何自动执行“csv”文件读取,其中我必须每五分钟读取一次文件并使用Pandas应用一些操作。我不想手动读取每个文件并为每个文件命名,然后在这些文件上应用函数(自定义)。我是编程的初学者。这些是我想在读取文件后应用的功能。提前谢谢!

df_9May = PreprocessDataframe(df_9May) #calling the function for the 9th May DF 



'''Reading the new DataFrame''' 

df_10May = pd.read_csv('fo10MAY2018bhav.csv', parse_dates = True)
df_10May = PreprocessDataframe(df_10May)


df_9_10 = combineDFs(df_9May, df_10May)
#print("count = {}".format(count))

df_9_10 = NewNetVal_AvgPrice(df_9_10)



df_11May = pd.read_csv('fo11MAY2018bhav.csv')
df_11May = PreprocessDataframe(df_11May)


df_10_11 = combineDFs(df_9_10, df_11May)

1 个答案:

答案 0 :(得分:0)

使用时间模块睡眠并定期执行任务(在这种情况下为5分钟) 提供文件夹/目录路径并使用os.listdir(路径),您可以获取文件名列表。

import time, os
path = input("CSV Files directory path")
flist = [file for file in os.listdir(path) if file.endswith('.csv')]

while True:  # Runs indefinitely or you could assign a counter n-times
  for file in flist:
    # Read the CSV File using pandas
    # perform your custom operations
  time.sleep(300) # 300 Seconds