I’m trying to add a space before every capital letter, except the first one.
Here’s what I have so far, and the output I’m getting:
>>> tex = 'ThisFileName.txt' >>> re.sub('[A-Z].', ' ', tex) ' his ile ame.txt'
I want: ‘This File Name.txt’
(It’d be nice if I could also get rid of .txt, but I can do that in a separate operation.)
Key concept here is backreferences in regular expressions:
For pulling off the ‘.txt’ in a reliable way, I recommend
os.path.splitext()