user3015703 user3015703 - 5 months ago 15
Python Question

How to replace consecutive spaces in a string in python

I have a awfully formatted data file with data structure as below:

" id1 id2 id3 id4"
" id1 id2 id3 id4"
" id1 id2 id3 id4"


I should retrieve id2 and id4 in each line, but number of spaces in each line between ids differs. Is there a way i could replace all consecutive spaces in each line with some character like '/t' so i could retrieve second and fourth item in each line?!
I appreciate any help.

Answer

The simplest way would be to do a .split() which automatically splits on any number of whitespace characters and ignores leading and trailing whitespace:

>>> s = " id1    id2             id3         id4"
>>> items = s.split()
>>> items
['id1', 'id2', 'id3', 'id4']

That way, you can access items[1] and items[3] directly. If you want to rebuild them into a tab-separated string, use .join():

>>> "\t".join(items)
'id1\tid2\tid3\tid4'