This is the input entered "S H A N N O N B R A D L E Y" (one space between each letter, but 2 spaces between SHANNON and BRADLEY) I want the output to be in this format (below)
"S H A N N O N B R A D L E Y"
SHANNON BRADLEY
Any way to do this in R or Python
try this in R
R
text <- c("S H A N N O N B R A D L E Y") gsub(" (?! )" , text , replacement = "" , perl = T)
this is another simple
gsub("\\b " , text , replacement = "" , perl = T)
If all letters are separated by one space and by two words in all words, you can do something like this:
In [2]: "S H A N N O N B R A D L E Y".replace(' ', ' ')[::2] Out[2]: 'SHANNON BRADLEY'
python, :
import re s = "S H A N N O N B R A D L E Y" ' '.join([''.join(i.split()) for i in re.split(r' {2,}',s)])
Python:
import sys myText="S H A N N O N B R A D L E Y" for i in range(len(myText)): if (myText[i]!=" " or myText[i+1]==" "): sys.stdout.write(myText[i]) else: pass
:
" ".join(["".join(w.split(" ")) for w in "S H A N N O N B R A D L E Y".split(" ")])
.
: , , .
Another option in Rc gsub:
gsub
gsub(" ([^ ])", "\\1", "S H A N N O N B R A D L E Y") # [1] "SHANNON BRADLEY"
In R:
x <- "S H A N N O N B R A D L E Y" paste(gsub(' ', '', unlist(strsplit(x, split = ' '))), collapse = ' ')
Python implementation:
import re re.sub(' (?! )', '', "S H A N N O N B R A D L E Y") #"SHANNON BRADLEY"
In python
In [1]: s="S H A N N O N B R A D L E Y" In [2]: "".join( [i.replace(' ','') if i !='' else i.replace('',' ') for i in s.split(' ') ]) Out[2]: 'SHANNON BRADLEY'