Souvik Ray
Souvik Ray

Reputation: 3028

How to remove whitespaces at certain sections of a text in Python?

I have a text which looks like below     

        High     MM Pol Ag to SO Pol Ag

As you can see, there is a whitespace in the beginning, then a word and again some whitespace and then the rest of the text.

What I want is this piece of text MM Pol Ag to SO Pol Ag.

Now I can use strip to remove the leading and ending whitespaces like below.

text = text.strip()

High     MM Pol Ag to SO Pol Ag

But the white space after High is varying ie it can sometimes be two spaces or four or maybe more.

How can I get the required text?

Note: The text can vary.

Upvotes: 2

Views: 46

Answers (3)

Selcuk
Selcuk

Reputation: 59315

You can split your string to remove spaces, then reconstruct it ignoring the first word:

>>> text = "        High     MM Pol Ag to SO Pol Ag"
>>> " ".join(text.split()[1:])
'MM Pol Ag to SO Pol Ag'

or, using a regex

>>> import re
>>> re.match("^\s*\w*\s*(.*)", text).group(1)
'MM Pol Ag to SO Pol Ag'

Upvotes: 2

Mark
Mark

Reputation: 92450

split() takes a maxsplit argument. This lets you split off just one piece of text and not split the rest:

s = "        High     MM Pol Ag to SO Pol Ag"

val = s.split(maxsplit=1)[1]  
print(val)
# 'MM Pol Ag to SO Pol Ag'

This lets you avoid creating a temp list and rejoining it to a string.

Upvotes: 1

Jakub Szlaur
Jakub Szlaur

Reputation: 2132

Here you go (use the split() method to get an array of strings):

s = "High     MM Pol Ag to SO Pol Ag"
    
s_new = ''.join([str(elem)+' ' for elem in s.split(' ')[5:]])

print(s_new)

Output:

MM Pol Ag to SO Pol Ag 

Upvotes: 0

Related Questions