简体   繁体   中英

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.

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'

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.

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM