-2

I am webscraping descriptions under images from a website and I am currently trying to scrape the descriptions using BeautifulSoup.

But that is working, some of the descriptions have one or more trailing spaces that I want to get rid of. How do I check if a string in a list has any trailing spaces and then to get rid of all the trailing spaces in each string of the list?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • You can use `rstrip()` method. You can read more about it here - https://www.geeksforgeeks.org/python-string-rstrip/ – Dhaval Taunk May 26 '20 at 14:00
  • Or even `rstrip()` if they're trailing – David Buck May 26 '20 at 14:00
  • or `strip()` is you want to remove both leading and trailing whitespace – Heike May 26 '20 at 14:01
  • 2
    Does this answer your question? [Remove leading and trailing spaces?](https://stackoverflow.com/questions/10443400/remove-leading-and-trailing-spaces) or even better: https://stackoverflow.com/questions/3232953/python-removing-spaces-from-list-objects – Tomerikoo May 26 '20 at 14:01
  • 1
    Does this answer your question? [Python: Removing spaces from list objects](https://stackoverflow.com/questions/3232953/python-removing-spaces-from-list-objects) – Hanan Hazani May 26 '20 at 14:02
  • 1
    Googling your exact title will give many results and many good, relevant answers. Did you even try that before asking here? – Tomerikoo May 26 '20 at 14:04
  • To the last comments posted, I did search it and i just wanted to get a better explanation for my case. My apologies for creating a duplicate. – Bhavya Patel May 26 '20 at 14:19

1 Answers1

1
stripped_list = [element.rstrip() for element in original_list]
jsbueno
  • 99,910
  • 10
  • 151
  • 209