Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Why doesn't strip work?
#1
import string
dogs = "All.Good,Dogs.. Eat.! Shoes?"
dogs = dogs.strip(string.punctuation)
print(dogs)
My result is All.Good,Dogs.. Eat.! Shoes
with no punctuation removed. Correction: The ? disappeared but I thought all punctuation would go.
Quote
#2
str.strip() only removes characters or space from the ends of the string. Notice that the question mark is removed from your string.

You want to use str.replace() to remove interior characters.
Quote
#3
Can do it like this.
>>> from string import punctuation
>>> 
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> ''.join(c for c in s if c not in punctuation)
'AllGoodDogs Eat Shoes'
Or specify yourself what to remove.
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> "".join(c for c in s if c not in list(',.!'))
'AllGoodDogs Eat Shoes?'
Quote
#4
Thanks all. Pydoc is Really unclear on that. It makes it look like it replaces all characters.
strip(...)
S.strip([chars]) -> str

Return a copy of the string S with leading and trailing
whitespace removed.
If chars is given and not None, remove characters in chars instead.

(Dec-26-2018, 10:28 PM)snippsat Wrote: Can do it like this.
>>> from string import punctuation
>>> 
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> ''.join(c for c in s if c not in punctuation)
'AllGoodDogs Eat Shoes'
Or specify yourself what to remove.
>>> s = "All.Good,Dogs.. Eat.! Shoes?"
>>> "".join(c for c in s if c not in list(',.!'))
'AllGoodDogs Eat Shoes?'

That looks like a list comprehension. I thought you had to use square brackets for that. ie:
import string
dogs = 'My., bad? dog - eats,,.. shoes*'
''.join([dog for dog in dogs if dog not in string.punctuation])

By the way, where two words were joined with punctuation and no space they were mashed together, but if I join with spaces I get too many. What's the best way to turn multiple spaces into one?
Quote
#5
(Dec-27-2018, 12:51 AM)cybervigilante Wrote: That looks like a list comprehension. I thought you had to use square brackets for that. ie:
It will be a generator expression.
>>> import string
>>> 
>>> dogs = 'My., bad? dog - eats,,.. shoes*'
>>> (dog for dog in dogs if dog not in string.punctuation)
<generator object <genexpr> at 0x03EEB830>

So eg calling join(), list() on will give result.
list comprehension will work,but needed in this case.
Quote:By the way, where two words were joined with punctuation and no space they were mashed together, but if I join with spaces I get too many. What's the best way to turn multiple spaces into one?
If it always same amount of spaces can use replace().
>>> 'My bad dog  eats shoes'.replace('  ', '')
'My bad dogeats shoes'
With different amount spaces i would use regex.
>>> import re
>>> 
>>> dogs = 'My   bad dog  eats      shoes'
>>> re.sub(' +',' ', dogs)
'My bad dog eats shoes'
Quote
#6
(Dec-27-2018, 12:51 AM)cybervigilante Wrote: By the way, where two words were joined with punctuation and no space they were mashed together, but if I join with spaces I get too many. What's the best way to turn multiple spaces into one?
This method would translate the target punctuation to spaces, then split the string by spaces and rejoin them to make one space between each word.
def remove_punc(s):
    from_ = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    to = '                                '
    translation = str.maketrans(from_,to)
    new = s.translate(translation)
    return ' '.join(new.split())
    
print(remove_punc("All.Good,Dogs.. Eat.! Shoes?"))
print(remove_punc('My., bad? dog - eats,,.. shoes*'))
Output:
All Good Dogs Eat Shoes My bad dog eats shoes
snippsat likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Serial loopback with Arduino doesn't work ThomasS 3 81 Yesterday, 12:47 PM
Last Post: deanhystad
  My Python Console doesn´t work ModuleNotFoundError: No module named 'tokenize' RuanKishibe 1 185 Aug-06-2020, 10:07 PM
Last Post: deanhystad
  Line of code to show dictionary doesn't work MaartenRo 2 278 Jul-28-2020, 03:58 PM
Last Post: deanhystad
  bool b = (num == 100) this doesn't work? MelonMusk 2 266 Jun-12-2020, 02:18 AM
Last Post: bowlofred
  len() function, numbers doesn't work with Geany Editor Penguin827 3 400 May-08-2020, 04:08 AM
Last Post: buran
  import scalalib package doesn't work manu_brighter 2 390 Apr-17-2020, 06:36 PM
Last Post: snippsat
  Need help with code for my WS2812B (Neopixel) Led Strip Phibbl 1 264 Apr-08-2020, 07:18 PM
Last Post: deanhystad
  Why my lambda doesn't work properly? Snake 6 684 Mar-29-2020, 04:26 PM
Last Post: Snake
  Multiprocessing doesn't seem to work? t4keheart 10 749 Feb-13-2020, 06:51 PM
Last Post: t4keheart
  why my function doesn't work cimerio 4 589 Jan-20-2020, 08:11 PM
Last Post: cimerio

Forum Jump:


Users browsing this thread: 1 Guest(s)