Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Re.Subs Python

Tags:

python

Sorry, but I can't figure this out from the Python documentation or any of the stuff I've found from Google.

So, I've been working on renaming files with code 99% from one of the awesome helpers here at StackOverflow.

I'm working on putting together a renaming script that (and this is what I got help with from someone here) works with the name (not the extension).

I'm sure I'll come up with more replacements, but my problem at the moment is that I can't figure out how to do more than one re.sub. Current Code (Replaces dots with spaces):

import os, shutil, re

def rename_file (original_filename):
    name, extension = os.path.splitext(original_filename)
    #Remove Spare Dots
    modified_name = re.sub("\.", r" ", name)
    new_filename = modified_name + extension
    try:
        # moves files or directories (recursively)
        shutil.move(original_filename, new_filename)
    except shutil.Error:
        print ("Couldn't rename file %(original_filename)s!" % locals())
[rename_file(f) for f in os.listdir('.') if not f.startswith('.')]

Hoping to also

re.sub("C126", "Perception", name)
re.sub("Geo1", "Geography", name)

Also, it'd be awesome if I could have it capitalize the first letter of any word except "and|if"

I tried

modified_name = re.sub("\.", r" ", name) && re.sub(... 

but that didn't work; neither did putting them on different lines. How do I do all the subs and stuff I want to do/make?

like image 835
Robin Hood Avatar asked Oct 27 '25 04:10

Robin Hood


1 Answers

Just operate over the same string over and over again, replacing it each time:

name = re.sub(r"\.", r" ", name)
name = re.sub(r"C126", "Perception", name)
name = re.sub(r"Geo1", "Geography", name)

@DanielRoseman is right though, these are literal patterns that don't need regexes to be described/found/replaced. You can use timeit to demonstrate plain old replace() is preferrable:

In [16]: timeit.timeit("test.replace('asdf','0000')",setup="test='asdfASDF1234'*10")
Out[16]: 1.0641241073608398

In [17]: timeit.timeit("re.sub(r'asdf','0000',test)",setup="import re; test='asdfASDF1234'*10")
Out[17]: 6.126996994018555
like image 130
Eduardo Ivanec Avatar answered Oct 30 '25 16:10

Eduardo Ivanec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!