Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Using str.replace with a wildcard

Tags:

python

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()
like image 568
baldurmen Avatar asked Dec 09 '13 03:12

baldurmen


People also ask

Can you use wildcards in Find and Replace?

To use wildcards, you will need to use the Find and Replace dialog box and expand it to display more options. You can then select the option to Use wildcards. A wildcard can replace one or more characters in a string of text or numbers. The most common wildcard is the asterisk (*).

How do you replace a pattern in a string in Python?

To replace a string in Python, the regex sub() method is used. It is a built-in Python method in re module that returns replaced string. Don't forget to import the re module. This method searches the pattern in the string and then replace it with a new given expression.

Can you use wildcard in Python?

In Python, we can implement wildcards using the regex (regular expressions) library.

What does str replace do in Python?

The replace() method returns a copy of the string where the old substring is replaced with the new substring.

How to replace a string with a different string in Python?

Therefore, the replace () method just replaces the first occurrence of the string 'bye' with the string 'baby' Use the Python string replace () method to return a copy of a string with some or all occurrences of a substring replaced by a new substring.

How to replace a pandas series with STR in Python?

Pandas Series.str.replace () method works like Python .replace () method only, but it works on Series too. Before calling .replace () on a Pandas series, .str has to be prefixed in order to differentiate it from the Python’s default replace method. Attention geek!

What is a wildcard string in Python?

Wildcard search in a string in Python The wildcard name comes from a card game, where a single card can represent any other card. The wildcard metacharacter is similar. It is represented by a dot (.) and matches any character, except for a new line character (\n).

What is the use of the replace() method in Python?

The replace () method returns a copy of a string with some or all matches of a substring replaced with a new substring. substr is a string that is to be replaced by the new_substr. count is an integer that specifies the first count number of occurrences of the substr that will be replaced with the new_substr. The count parameter is optional.


2 Answers

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

e.g.:

>>> re.sub(r'\(.*\)', '', 'foobar (###)')
'foobar '
like image 100
mgilson Avatar answered Oct 21 '22 22:10

mgilson


If the "(###)" is always at the end.

output = foldername.rpartition("(")[0]
like image 26
John La Rooy Avatar answered Oct 21 '22 21:10

John La Rooy