Saranjit Saikia
Saranjit Saikia

Reputation: 83

Remove White space before and after a special character and join them python

My_string =  My Awesome Company billing @ example . com Contractor Invoice # 000015 Acme Projects - Taxable Product Contractor Invoice Summary Account Information Don Test don @ example . com Contractor Invoice Date : 10 / 26 / 2016 Amount Due $ 21 .


Desired_string = My Awesome Company [email protected] Contractor Invoice#000015 Acme Projects-Taxable Product Contractor Invoice Summary Account Information Don Test [email protected] Contractor Invoice Date:10/26/2016 Amount Due$21.

In simple words I need to remove space from before and after a special character. Also can you share a good source to learn regex from

with open('sentence.txt') as txtfile:
string = str(txtfile.read())
list_of_str = string.split()
new_list = []
for d in range(len(list_of_str)):
    if not (list_of_str[d].isalpha() or list_of_str[d].isalnum()):
       print(list_of_str[d-1], list_of_str[d:])
       new_list.append(str(list_of_str[d-1]) + str(list_of_str[d]) + str(list_of_str[d+1]))
    else:
        new_list.append(list_of_str[d])
print(new_list)

Output: ['OnlineMyAwesome', 'Awesome', 'Company', 'billing', 'billing@example', 'example', 'example.com', 'com', 'Contractor', 'Invoice', 'Invoice#000015', '000015', 'Acme', 'Projects', 'Projects-Taxable', 'Taxable', 'Product', 'Contractor', 'Invoice', 'Summary', 'Account', 'Information', 'Don', 'Test', 'don', 'don@example', 'example', 'example.com', 'com', 'Contractor', 'Invoice', 'Date', 'Date:10', '10', '10/26', '26', '26/2016', '2016', 'Amount', 'Due', 'Due$21', '21']

At first i tried to use this but I think regex can help

Thank you

Upvotes: 2

Views: 1799

Answers (1)

Pushpesh Kumar Rajwanshi
Pushpesh Kumar Rajwanshi

Reputation: 18357

Yes, you can easily solve this problem using regex, rather than you current code.

You can use this regex,

([@.#$\/:-]) ? (Space followed by character set having special chars followed by an optional space. You can add more characters in the set as per your needs.)

This regex captures a space followed by one character in your character set followed by optional space and replaces it with the character it captured in group 1.

Demo

Sample python codes,

import re
s = 'My Awesome Company billing @ example . com Contractor Invoice # 000015 Acme Projects - Taxable Product Contractor Invoice Summary Account Information Don Test don @ example . com Contractor Invoice Date : 10 / 26 / 2016 Amount Due $ 21 .'
s = re.sub(' ([@.#$\/:-]) ?',r'\1', s)
print(s)

which gives following output,

My Awesome Company [email protected] Contractor Invoice#000015 Acme Projects-Taxable Product Contractor Invoice Summary Account Information Don Test [email protected] Contractor Invoice Date:10/26/2016 Amount Due$21.

Let me know if this works fine for you.

Upvotes: 1

Related Questions