Python Forum
How I could do that in the best way?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How I could do that in the best way?
#4
This is just checking how many parts the path has. The first part is always /.

from pathlib import Path
from urllib.parse import urlparse

def how_many(url):
    return len(Path(urlparse(url).path).parts) - 1


print(how_many("http://{ }/something/{ }/{ }/{ }/{ }"))
Output:
5
You can also do this to count the parts:
len(Path("http://{ }/something/{ }/{ }/{ }/{ }".removeprefix("http://")).parts) - 1
str.removeprefix was added since Python 3.9
Kalet and carecavoador like this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
How I could do that in the best way? - by Kalet - Dec-06-2022, 07:00 AM
RE: How I could do that in the best way? - by Kalet - Dec-06-2022, 12:54 PM
RE: How I could do that in the best way? - by DeaD_EyE - Dec-06-2022, 03:35 PM
RE: How I could do that in the best way? - by Kalet - Dec-06-2022, 04:58 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020