Welcome folks today in this blog post we will be removing special characters
from the 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 make an app.py
file and copy paste the following code
app.py
1 2 3 4 |
import re string = open('input.txt').read() new_str = re.sub('[^a-zA-Z0-9\n\.]','', string) open('b.txt', 'w').write(new_str) |
And now we need to create the input.txt
file before you execute the python
script and copy paste the below text which contains the special characters
as shown below
input.txt
1 2 3 4 |
@###@@@dfsdfsd sdfsdf @##fdsfsdfsdf sdfsdf$$sfsd |
And after that you need to execute the above python
script using the below command as shown below
python app.py