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?
#1
Hi, I would like to know if there is a way to validate the '{}' when I receive them in a function. For example, I receive a URL and I have two parameters and I can perfectly do the .format. However, what if I have several URLs and some of them have the following model:

import re
def test(param):
        # I need here know how many {} I will receive
        print(param.format("google.com", "webpath")) 

test("http://{}/something/{}")
# What's happen if I will do the next?
#test("http://{}/something/{}/{}/{}/{}")
It is for this reason, that maybe it could be done with re.replace, but it seems to me a rudimentary way and even so, I think it would not solve the problem:

import re
def test(param):

        print(param.format(param.replace('[host]', 'a'), param.replace('[path]',"b")))

test("http://[host]/something/[path]")
Any ideas?

Thank you!
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 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