-1

I feel like I have a very straightforward piece of code. I have a file name that follows the form of 'stuff_category.csv'. I'm trying to remove 'stuff_' and '.csv', so that I will be left with 'category', which I need for the next piece of code. 'stuff_' can be many different things, so I can't use the replace() function. Right now I have

filename = "stuff_category.csv"
category = filename.lstrip('_').rstrip('.')

But if I try print(category), or even print(category.lstrip('_')), it just returns the original file name. What am I missing?

user2954167
  • 155
  • 1
  • 3
  • 14
  • 6
    You missed reading the documentation about how [`.lstrip()`](https://docs.python.org/3/library/stdtypes.html#str.lstrip) and [`.rstrip()`](https://docs.python.org/3/library/stdtypes.html#str.rstrip) work. – Mark Tolonen Jun 22 '21 at 23:11
  • 2
    If you want to extract base name and/or file extension use `os.path.splitext(filename)` which returns ('stuff_category', '.csv'). – CodeMonkey Jun 22 '21 at 23:18
  • @MarkTolonen I didn't "miss" it, I guess I just misunderstood how it works. Everything I saw said when lstrip() is given an argument, it removes everything before that argument. – user2954167 Jun 22 '21 at 23:34
  • I found that 'filename.split('.')[0].split('_')[-1]' produces the result I'm looking for. It's not as pretty as I was hoping for, but it gets the job done. – user2954167 Jun 22 '21 at 23:43
  • @user2954167 The linked official docs give clear examples of how it works. – Mark Tolonen Jun 22 '21 at 23:51

2 Answers2

1

You could do it using removeprefix and removesuffix (Python 3.9) rather than lstrip and rstrip:

filename = "stuff_category.csv"
print(filename.removeprefix('stuff_').removesuffix('.csv')) #category

Or using Slicing with startswith endswith (Python 3.8):

start = "stuff_"
end = ".csv"
if filename.startswith(start):
    filename = filename[len(start):]
if filename.endswith(end):
    filename = filename[:-len(end)]
print(filename) #category

Or even use Slicing with just index:

print(filename[filename.index('_')+1:filename.index('.')]) #category
XMehdi01
  • 5,538
  • 2
  • 10
  • 34
-1

You're missing the documentation for these methods. They don't use the provided character as a delimiter, they remove the characters as if a substring.

lstrip(self, chars=None, /)
    Return a copy of the string with leading whitespace removed.
    
    If chars is given and not None, remove characters in chars instead.

Try this:

filename = "stuff_category.csv"
category = filename.lstrip('stuff_').rstrip('.csv')

Consider using a regular expression or str.split instead of lstrip/rstrip if "stuff_" isn't constant.

Michael Ruth
  • 2,938
  • 1
  • 20
  • 27
  • 5
    Your example isn't really how it works either. `.lstrip('stuff_')` for example removes all *combinations* of s, t, u, f and _ from the right, so `"stuff_futs_category".lstrip('stfu_')` would return `"category"`. – Mark Tolonen Jun 22 '21 at 23:18
  • 3
    Python 3.9 introduces `removeprefix` and `removesuffix` methods that do what everyone always thinks `lstrip` and `rstrip` do before they realize their mistake. – user2357112 Jun 22 '21 at 23:19
  • @MarkTolonen thanks for the correction – Michael Ruth Jun 22 '21 at 23:30
  • 1
    @user2357112supportsMonica the `str.removeprefix` and `str.removesuffix` methods are indicated here, and they're easy for OP to implement if not using 3.9 – Michael Ruth Jun 22 '21 at 23:33
  • I wanted to avoid using str.split. Then I have to add a step of finding the right item in the list, and that's a bit bulkier than I wanted. But I suppose if that's the only way, then that's what I have to work with. – user2954167 Jun 22 '21 at 23:39
  • 1
    @user2954167, definitely try something other than the strip methods. For reasons mentioned above they will likely produce bugs in this case. – Michael Ruth Jun 22 '21 at 23:46