I am using Python 3 to process file names, and this is my code:
name = 'movies.csv'
table_name = name.strip(".csv")
The expected value of table_name should be “movies” yet table_name keeps returning “movie”.
Why is it doing this?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
strip() removes all the leading and trailing characters from the input string that match one of the characters in the parameter string:
>>> "abcdefabcdefabc".strip("cba")
'defabcdef'
You want to use a regex: table_name = re.sub(r".csv$", "", name) or os.paths path manipulation functions:
>>> table_name, extension = os.path.splitext("movies.csv")
>>> table_name
'movies'
>>> extension
'.csv'
Method 2
Maybe a bit late, but for those reading this in the future, there is a lazy way that appears to work fine too (assuming all the files from which you want to retrieve the names are CSV files):
if name.endswith('.csv'):
table_name = name.rstrip("csv").rstrip(".")
As said in other solutions, the strip() method removes all the leading/trailing characters that match those inside the parentheses. Therefore, the idea in this approach is to:
- Remove the
csvextension – since there is a.we knowrstrip()will stop searching there. This will leave us with themovies.string. - Remove the
.from themovies.string – therstrip()will only look for trailing dots.
Why rstrip(): Since we know that the text to be removed is in the end of the string, we can specify rstrip for better control (i.e. to avoid unintentionally removing any eventual leading c, s or v characters)
Method 3
I don’t know what you need is, but if it’s retrieving filename without extension, you have the os.path.splitext function:
>>> import os
>>> name, extension = os.path.splitext("movies.csv")
>>> name
'movies'
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0