Link to home
Start Free TrialLog in
Avatar of Konstantinos Vouzoplis
Konstantinos Vouzoplis

asked on

How to remove special characters from text file using python

Hello Experts,

i have hundreds of txt files that i need to work with for a specific project.  These text files are in folder c:\textfiles\
i need a python script that will remove any special characters but to leave the period, question mark, exclamation point, and comma.  The specific characters that i need removed are @ / \ [ ] < >  * - _.
The process that this might follow is,
1.open file
2.read file
3.remove special characters
4.save file with same file name

but this needs to work automatically throughout the entire specified directory.
the version of python im using is 2.7.3 on a windows machine.
Avatar of aikimark
aikimark
Flag of United States of America image

@ / \ [ ] < >  * - _.
Is the period character part of your to-be-removed list?
Have you used regular expressions before?
ASKER CERTIFIED SOLUTION
Avatar of gelonida
gelonida
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Konstantinos Vouzoplis
Konstantinos Vouzoplis

ASKER

hello!
@aikimark, the period needs to remain, as it is one of the differentiators amongst sentences, and yes, i have used regex's before..

@gelonida, im going to try your script in a few minutes
@gelonida,

hello.
the text files are all the same, simple ascii txt files.
i want to remove majority of special characters, but want to keep the . , ! ?