Python - Using str.replace with a wildcard

baldurmen

I'm trying to rename folder names named with this pattern: FOLDERNAME (###)

I'm trying to get rid of (###), a series of numbers of random length.

I would like to use str.replace as show below to do it, but I'm not sure I can use a wildcard this way...

folderdir = os.listdir(path)            # Listing the folder names
for foldername in folderdir:
    output = foldername.replace("(*)", "")
    rename()
mgilson

Nope, str.replace won't work. You need re.sub.

e.g.:

>>> re.sub(r'\(.*\)', '', 'foobar (###)')
'foobar '

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript str.replace with wildcard/regex?

From Dev

wildcard search using replace function

From Dev

Using a function in str_replace

From Dev

Python Using wildcard inside of strings

From Dev

Select columns with name matching str with wildcard for t-test (Python)

From Dev

vim swap substrings while using a wildcard in search and replace

From Dev

Find and replace using wildcard/regex characters in Notepad++

From Dev

Excel: How do I replace text by using a wildcard?

From Dev

Using javascript's replace with wildcard to change width attribute inside string

From Dev

Issue with using exslt str:replace with a nodeset

From Dev

Using double quotes on str_replace

From Dev

PHP str_replace tags with using arrays

From Dev

Using str_replace to alter the_title()

From Dev

Foreach loop using multiple str_replace

From Dev

failed when using str_replace

From Dev

python replace string function throws asterix wildcard error

From Dev

Python find tag in XML using wildcard

From Dev

Python find tag in XML using wildcard

From Dev

Using Wildcard in CSV lookup with if/else statement in python

From Dev

Python str.translate VS str.replace

From Dev

str_replace_all() r equivalent in python

From Dev

How to use str.replace in Python 3

From Dev

How to replace multiple strings in PHP using str_replace?

From Dev

PHP replace multiple value using str_replace?

From Dev

How to replace multiple strings in PHP using str_replace?

From Dev

Replace two "words" in a string using str_replace

From Dev

Trying to replace a () in a string in R using str_replace

From Dev

MySQL for replace with wildcard

From Dev

javascript wildcard replace function

Related Related

HotTag

Archive