how to remove the timestamp value from a file present in a list in python

365 views Asked by At

I have to remove timestamp value from files say

(abc_20200124T141028.csv, bcd_20200124T141052) i.e.,this value(20200124T141028) present in a list a[]

How will I do?

1

There are 1 answers

1
Grom On BEST ANSWER

you can split the name on the underscore using rsplit() and go to lower() to compare

ex:

name = 'HER_ADDRESS_data_20200124T141028.csv'
filename_noext, ext = name.rsplit("_",1)
# perform your test and do something
filename_noext.lower() + '.csv' == 'her_address_data.csv' # True

edited based on the clarification that it is a list of names. Just do it one by one?

namelist = ['HER_ADDRESS_data_20200124T141028.csv', 'NOT_HER_ADDRESS_data_20200124T141028.csv']

for name in namelist:
  filename_noext, ext = name.rsplit("_",1)
  # perform your test and do something
  filename_noext.lower() + '.csv' == 'her_address_data.csv' # True