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
  why my function doesn't work cimerio 4 187 Jan-20-2020, 08:11 PM
Last Post: cimerio
  Doesn't work function pyautogui.typewrite() aliyevmiras 1 137 Dec-22-2019, 11:35 AM
Last Post: aliyevmiras
  I don't understand why this doesn't work sandeen 4 171 Dec-19-2019, 07:52 PM
Last Post: sandeen
  For loop in my __init__ doesn't work as expected Jessy 2 147 Nov-18-2019, 10:07 AM
Last Post: buran
  split strip issues 'NonType' casacafe 8 240 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  Python: command “python -m pip install --upgrade pip” doesn't work apollo 2 415 Sep-16-2019, 03:11 PM
Last Post: snippsat
  removing spaces/tabs after used .strip() zarize 0 145 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 612 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  Why doesn't my loop work correctly? (problem with a break statement) steckinreinhart619 2 417 Jun-11-2019, 10:02 AM
Last Post: steckinreinhart619
  Python Code doesn't work! the_entrepreneur 5 553 Apr-20-2019, 08:05 AM
Last Post: SheeppOSU

Forum Jump:


Users browsing this thread: 1 Guest(s)