I got stuck a little when deleting a string in Python, so I wrote it as a reminder
You may find yourself casually using rstrip, lstrip.
You may want to use lstrip or rstrip when removing prefixes or suffixes in filenames, as shown below.
fileName='test_Output file_20191217_001.xlsx'
#Remove prefix
print(fileName.lstrip('test_'))
#Output file_20191217_001.xlsx
#Remove suffix
print(fileName.rstrip('.xlsx'))
# test_Output file_20191217_001
There seems to be no problem as an output result, but a problem occurs when the character string is as follows.
fileName='test_est_Output file_20191217_001xsls.xlsx'
#Remove prefix
print(fileName.lstrip('test_'))
#Output file_20191217_001xsls.xlsx
#Remove suffix
print(fileName.rstrip('.xlsx'))
# test_est_Output file_20191217_001
I want only test_ or .xlsx to disappear, but other characters have also disappeared.
The cause is that lstrip or rstrip deletes the one that matches the specified string.
For patterns erased with a prefix, one of the characters in test_ is deleted.
If it is a suffix, one of the .xlsx is deleted.
If you want to remove the prefix or suffix in these cases
It seems good to use re to remove it with a regular expression.
In the case of the example
import re
fileName='test_Output file_20191217_001.xlsx'
#Remove prefix
print(re.sub("^test_", "", fileName))
#Output file_20191217_001.xlsx
#Remove suffix
print(re.sub(".xlsx$", "", fileName))
# test_Output file_20191217_001
You can firmly remove the prefix or suffix.
Examples that didn't work with lstrip or rstrip also work, as shown below.
import re
fileName='test_est_Output file_20191217_001xsls.xlsx'
#Remove prefix
print(re.sub("^test_", "", fileName))
# est_Output file_20191217_001xsls.xlsx
#Remove suffix
print(re.sub(".xlsx$", "", fileName))
# test_est_Output file_20191217_001xsls
Recommended Posts