Python doesn't consider character "c" in a string

Viewed 69

I have a string like this:

mystring="Clusterd695c_ROUGE1.csv"

When I use mystring.rstrip("_ROUGE1.csv"), I expect to return "Clusterd695c", but I get "Clusterd695". As if the last character "c" has not been seen.This only happens for character "c" and the other characters works true. Like this:

mystring="Clusterd695f_ROUGE1.csv"
mystring.rstrip("_ROUGE1.csv")

Then I get "Clusterd695f", that was expecting.

How can I correct this?

1 Answers

This is a bit surprising to me but the issue is that rstrip, when provided a string, will treat that string as a set and remove characters from the end of the string until they do not belong to that set. Because there's a 'c' in the newly-created set (i.e., csv), it removes the 'c' at the end too, stopping at '5' because it doesn't belong to the string "_ROUGE1.csv".

One way to deal with this is to use a replace: mystring.replace("_ROUGE1.csv", ""); another option is to simply chop off the last len("_ROUGE1.csv") characters. One caveat with the replace approach is that it will replace that string anywhere in the string, so "_ROUGE1.csv_ROUGE1.csv".replace("_ROUGE1.csv", "") == ""

Related