Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all those Special Characters with white spaces in python?

How to replace all those special characters with white spaces in python ?

I have a list of names of a company . . .

Ex:-[myfiles.txt]

MY company.INC

Old Wine pvt

master-minds ltd

"apex-labs ltd"

"India-New corp"

Indo-American pvt/ltd

Here, as per the above example . . . I need all the special characters[-,",/,.] in the file myfiles.txt must be replaced with a single white space and saved into another text file myfiles1.txt.

Can anyone please help me out?

like image 243
Tyler Durden Avatar asked Jan 10 '12 09:01

Tyler Durden


2 Answers

Assuming you mean to change everything non-alphanumeric, you can do this on the command line:

cat foo.txt | sed "s/[^A-Za-z0-99]/ /g" > bar.txt

Or in Python with the re module:

import re
original_string = open('foo.txt').read()
new_string = re.sub('[^a-zA-Z0-9\n\.]', ' ', original_string)
open('bar.txt', 'w').write(new_string)
like image 96
Stephen Emslie Avatar answered Sep 30 '22 22:09

Stephen Emslie


import string

specials = '-"/.' #etc
trans = string.maketrans(specials, ' '*len(specials))
#for line in file
cleanline = line.translate(trans)

e.g.

>>> line = "Indo-American pvt/ltd"
>>> line.translate(trans)
'Indo American pvt ltd'
like image 30
dabhaid Avatar answered Sep 30 '22 22:09

dabhaid