matru878
matru878

Reputation: 33

Python replace string with whitespace

I have a test.txt file with following data:

jon doe smith 
\jon\-\1\ \jon\-\2\ \jon\-\3\ \doe\- \1\ \doe\-\2\ 
\doe\-\3\ \smith\- \1\ \smith\-\2\ \smith\-\3\

what i've been trying to get here is to remove the contents mentioned in a list "name" mentioned below code from the txt file.

with open(r'test.txt', 'r') as file:
    data = file.read()
print(data)
name=['jon','doe']

for e in name:
   new_string=data.replace('\\' + e + '\\-\\1\\',' ').replace('\\' + e + '\\-\\2\\',' ').replace('\\' + e + '\\-\\3\\',' ').replace('\\' + e + '\\',' ')

print(new_string)

the output of which this code presents:-

jon doe smith 
\jon\-\1\ \jon\-\2\ \jon\-\3\ \doe\- \1\ \doe\-\2\
\doe\-\3\ \smith\- \1\ \smith\-\2\ \smith\-\3\
jon doe smith
\jon\-\1\ \jon\-\2\ \jon\-\3\  - \1\
  \smith\- \1\ \smith\-\2\ \smith\-\3\

the output which i need:-

jon doe smith 
\jon\-\1\ \jon\-\2\ \jon\-\3\ \doe\- \1\ \doe\-\2\
\doe\-\3\ \smith\- \1\ \smith\-\2\ \smith\-\3\
         smith

           \smith\- \1\ \smith\-\2\ \smith\-\3\

Also Is there a way where instead of replace, regex can be used for this ?

Upvotes: 2

Views: 325

Answers (1)

hussic
hussic

Reputation: 1920

Try:

import re
pattern = r"\\?(\w+)\\?(- ?[\\\d]*)?"
res = re.sub(pattern, lambda x: x[0] if x[1] not in name else ' ', x)

Upvotes: 1

Related Questions