Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace special characters within a text with a space in Python?

Problem Statement: If a special character is found with alphabets, then replace it with one space. And, if it is found with digits, then simply ignore.

Actual Scenario:

  1. $45
  2. 4.5 inches
  3. Task.This is good.
  4. Hello, How $are you. This is good.

Expected Scenario:

  1. $45
  2. 4.5 inches
  3. Task This is good
  4. Hello How are you This is good

I have tried writing one regex to find out the texts which follows this pattern, but not sure exactly how to replace special characters in that text with a space.

enter image description here

For e.g. In above image, expected output would be 'ddddd dfhghg', '222 d' etc.

Can this scenario be handled by re.sub(pattern, replacement, input)? If yes, please tell how :)

like image 412
Deepak Avatar asked Jan 05 '19 22:01

Deepak


People also ask

How do you replace special characters in space in Python?

For example, to keep all alphanumeric characters and spaces, we simply tell the . sub() method to replace anything except for [^a-zA-Z0-9 ] .

How do you replace a character in a text in Python?

To replace text in a file we are going to open the file in read-only using the open() function. Then we will t=read and replace the content in the text file using the read() and replace() functions.

How do you replace all occurrences of a character in Python?

Replace all occurrences of a character in a string To replace all the occurrences of a character with a new character, we simply have to pass the old character and the new character as input to the replace() method when it is invoked on the string. You can observe this in the following example.

How to change a character in a string in Python?

The string is immutable in Python. We can’t change a character in a string directly. So, we need to create one different string using the characters of the provided string. During the iteration process, keep building the new string by joining the characters. If any replaceable character is found, replace it with the symbol and join it.

How to get rid of multiple spaces in a string in Python?

The easiest way to get rid of multiple spaces in a string is with a regular expression search using the Python sub()function from the re module. We can easily define a regular expression which will search for any number of spaces, and then using the sub()function, we will replace the multiple spaces with one space.

How to replace multiple spaces with single spaces in a string?

Multiple spaces between words in a string of text can make sentences and text unreadable. We can easily replace multiple spaces with single spaces in a string in Python. The easiest way to get rid of multiple spaces in a string is with a regular expression search using the Python sub()function from the re module.


1 Answers

You can use a character set with wrapped with negative lookarounds:

(?<!\d)([.,$])(?!\d) 

Put all the chars that should be replaced inside the brackets: [.,$]

Demo

Explanation:

  • (?<!\d)negative lookbehind - ensure that there is no digit just before a matched character
  • (?!\d)negative lookahead - ensure that there is no digit just after a matched character
  • [...] character set with all the special chars you want to replace
like image 55
mrzasa Avatar answered Sep 29 '22 07:09

mrzasa