Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Remove from end of string up to and including some character
#1
Hi all,

I'm trying to take some string, stri_ , and iterate through it until I find a particular character. Then, I will update the stri_ to be stri_ with that character and everything after it removed.

#  Working with url_ , a string of any length
url_ = url_[:url_.find(".")]

#  For url_ == "wikipedia.com", url_ should become "wikipedia".
print(url_)  # wikipedia
The above works fine in most cases, however I ran into problems with the below.

#  Working with url_ , a string of any length
url_ = url_[:url_.find(".")]

#  For url_ == "en.wikipedia.com", url_ should become "en.wikipedia".
print(url_)  # en
Is there any way I can do something similar to rstrip(), but instead of removing a single character, remove every character rstrip() checked before it found "." ie. the last "." in the string?

Thanks,

K
Quote
#2
You were on the right lines with rstrip. Instead use rsplit(), which takes two arguments. A delimiter and a maxsplit.

result = url_.rsplit('.', 1)[0]
It doesn't matter how many '.' you have at the front of the string, it will only every remove anything after the last '.':
Output:
>> a = "wikipedia.com" >> print(a.rsplit(".", 1)[0]) >> wikipedia >> a = "en.wkikipedia.com" >> print(a.rsplit(".", 1)[0]) >> en.wkikipedia >> a = "en.something.somethingelse.wikipedia.com" >> print(a.rsplit(".", 1)[0]) >> en.something.somethingelse.wikipedia
Quote
#3
change url_find('.') to url_rfind('.') will give you what you want
Output:
>>> url = 'wikipedia.com' >>> print(url[:url.rfind('.')]) wikipedia >>> url = 'en.wikipedia.com' >>> print(url[:url.rfind('.')]) en.wikipedia
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
Johnny's CookBook - Powered By: Python3.8 | Bootstrap4 | Apache2 | Django
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Remove escape characters / Unicode characters from string DreamingInsanity 5 356 May-15-2020, 01:37 PM
Last Post: snippsat
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 335 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Highlight and remove specific string of text itsalmade 5 485 Dec-11-2019, 11:58 PM
Last Post: micseydel
  How to get the index of a character from a string chihaya 1 295 Dec-03-2019, 12:54 PM
Last Post: buran
  "Up to but not including" (My personal guide on slicing & indexing) Drone4four 5 388 Nov-20-2019, 09:38 PM
Last Post: newbieAuggie2019
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 1,515 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  python gives wrong string length and wrong character thienson30 2 425 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  Applying row height to all rows including and after row 7 curranjohn46 2 565 Oct-14-2019, 03:10 PM
Last Post: curranjohn46
  with input remove a string from the list konsular 3 383 Oct-12-2019, 09:25 AM
Last Post: konsular
  Remove \n at the end of a character from a list judkil 2 503 Jun-24-2019, 12:15 AM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)