0

What I'm doing is loading a list of URLs from a .txt file which is working as it should:

def load_urls():
    try:
        input_file = open("filters\\inputLinks.txt", "r")
        for each_line in input_file:
            link = each_line.rstrip('\n')
            identify_platform(link)
    except Exception as e: 
        print("Loading URLs error: ", e)

def identify_platform(link):
    try:
        SEARCH_FOR = ["/node/", "/itemlist/"]
        if any(found in link for found in SEARCH_FOR):
            print(link)
    except Exception as e: 
        print("Identifying URLs error: ", e)

if __name__ == "__main__":
    load_urls()

It will then check if a URL contains one of the SEARCH_FOR array elements. If it does, we print it to screen. Is it possible to also print out which one of the array elements it found using something like:

print(element_found + "|" + link)
1
  • Ok, but what is element_found supposed to be? Commented May 25, 2019 at 16:28

2 Answers 2

2

It is possible if your separate into multiple lines, making it more readable:

for found in SEARCH_FOR:
    if found in link:
        print(found + '|' + link)
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of using the any operator, just do a list comprehension. Note this might give you 2 results if both of the search items are in the target.

matches = [(item, link) for item in SEARCH_FOR if item in link]
for match in matches:
    print(match[0] + '|' + match[1])

1 Comment

Thank you Jeff, that is exactly what i need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.