Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

change white space with regular expression in python

I need to change white space to a character, but only if there are two or more white spaces and there is only one I want to keep it.

An example of text is:
142526 0x8520003 2 2022-10-20 The interface status changes. (ifName=Gig.

I need:
142526;0x8520003;2;2022-10-20 The interface status changes. (ifName=Gig.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I use:

';'.join(headers.split())

but change one space white also. Thanks!!

>Solution :

Use re.split() to match more than one space.

import re

new_headers = ';'.join(re.split(r'\s{2,}', headers)

\s matches whitespace, and {2,} means to match 2 or more of them in a row.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading