1

I have a list of CSV file names that I am trying to get the date out of in order to put them into order by date

Heres a snippet of the file names

csse_covid_19_data/csse_covid_19_daily_reports/02-01-2020.csv
csse_covid_19_data/csse_covid_19_daily_reports/01-31-2020.csv
csse_covid_19_data/csse_covid_19_daily_reports/02-02-2020.csv
csse_covid_19_data/csse_covid_19_daily_reports/01-24-2020.csv
csse_covid_19_data/csse_covid_19_daily_reports/01-29-2020.csv

I can get the date from a single file using

a_date_string= all_files[0].split("/")[-1].split(".")[0]
print(a_date_string)

which gives output

02-01-2020

How do I get the code to return all the dates? The code above was given I am just trying to manipulate it to be in order by the dates listed in the file name.

CinCout
  • 8,291
  • 9
  • 47
  • 55

3 Answers3

3
dates = [i[-14:-4] for i in all_files]
print(dates)

See if this works for you. This would return a list of those dates.

If any doubts in this snippet let me know in comments.

Rishabh Kumar
  • 1,981
  • 3
  • 7
  • 21
2

Since you tagged the question regex, here is a possible solution:

/(\d{2}-\d{2}-\d{4})\.csv$

The resulting capture groups will contain the desired dates.

Demo

CinCout
  • 8,291
  • 9
  • 47
  • 55
0
a_date_string=[]
for file_name in all_files:
    a_date_string.append(file_name.split("/")[-1].split(".")[0])
print(a_date_string)

Assuming all_files is a list of all file names

manjari
  • 21
  • 6