Python 3 Script to Remove Special Characters From Text File Using Regular Expression Full Project For Beginners

You are currently viewing Python 3 Script to Remove Special Characters From Text File Using Regular Expression Full Project For Beginners

 

 

Welcome folks today in this post we will be removing special characters from text file using regular expression in python. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to create a text file inside the root directory called as a.txt and copy paste the following code

 

a.txt

 

@###@@@dfsdfsd
sdfsdf
@##fdsfsdfsdf
sdfsdf$$$$sfsd

 

 

We will be removing these special characters from the text file using python. So now create an app.py file and copy paste the following  code

 

app.py

 

 

import re
string = open('a.txt').read()
new_str = re.sub('[^a-zA-Z0-9\n\.]', ' ', string)
open('b.txt', 'w').write(new_str)

 

 

Now if you execute the above python script by typing the below command as shown below

 

python app.py

 

So it will create a output b.txt file as shown below

 

 

As you can see it has removed all the special characters from the text file

Leave a Reply