Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove part of string within a loop in python

Keep in mind this in within a loop.

How can I remove everything from "?" and so on?

So that "something_else_1" gets deleted

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Url_before = "https:www.something.com?something_else_1"


Url_wanted = "https:www.something.com?"

In practice it looks kinda like this:

find_href = driver.find_elements(By.CSS_SELECTOR, 'img.MosaicAsset-module__thumb___yvFP5')

with open("URLS/text_urls.txt", "a+") as textFile:
            for my_href in find_href:
                textFile.write(str(my_href.get_attribute("src"))+"#do_something_to_remove_part_after_?_in_find_href"+"\n")

>Solution :

Provided there’s only one instance of "?" in the string and you want to remove everything after it, you could find the index of this character with

i = Url_before.index("?")

and then remove everything after it:

Url_wanted = Url_before[:i+1]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading