Steve Kundukulangara
Steve Kundukulangara

Reputation: 77

read a csv file with specific pattern dynamically in pandas

I have some CSV files with its extension which keeps changes all the time.

  1. Active_Count_1618861363072
  2. Deposit_1618861402104
  3. Game_Type_Wise_Net_Sell_1618861383176
  4. Total_Count_1618861351976

I want to read these files automatically

df1=pd.read_csv('Active_count_'.csv)
df2=pd.read_csv('Deposit_'.csv)
df3=pd.read_csv('Game_Type_Wise_Net_Sell_'.csv)
df4=pd.read_csv('Total_Count_'.csv)

I want this in such a way that I want to keep after the underscore dynamic and load the CSV files. Is there a way I can achieve this?

Upvotes: 2

Views: 653

Answers (1)

Nicolas78
Nicolas78

Reputation: 5144

This can be achieved outside Pandas using only standard Python functionality:

import glob

active_count_filename = glob.glob('Active_Count_*.csv')[0]
df1 = pd.read_csv(active_count_filename)

This assumes that there is exactly one Active_count_* file - if none exists, it will throw an error, if more than one exists, one will be chosen randomly.

Upvotes: 1

Related Questions