Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7519929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:58:55+00:00 2026-05-30T01:58:55+00:00

I’m new to python (and in any real sense) to programming. I’ve been playing

  • 0

I’m new to python (and in any real sense) to programming. I’ve been playing around with this problem and here’s my extended solution to repeatedly check a whitelist file to see whether user input domains are whitelisted (one at a time). The whitelist file contains a list of domains (one per record).

It appears to work but I got there by trial and error. Can this be done in a more elegant, efficient and ‘pythonic’ manner? I’ve not got round to error checking yet (if that is relevant). Any tips gratefully received.

#!/usr/bin/env python3
# Filename: whitelist.py

domain = 1
match = 0
while domain:
    domain = input('Enter a domain or press return to exit: ') 
    if not domain: # if return is pressed entered then end program
        break
    with open('whitelist.txt', 'r', encoding='utf-8') as whitelist:
        for recd in whitelist: 
            # if input domain doesn't match record read next record (continue)
            if recd.lower().rstrip() != domain.lower():  
                continue
            else: # otherwise if match set match indicator
                match = 1
            break # after setting indicator stop reading records

        if match:
            print('Match on domain: ', domain)                
        else:   
            print('No match on: ', domain)

After taking on board larsmans’ comments my new solution is basically his solution with a slight change to make all checks use lower case (e.g. so cnn.com == CNN.com):

#!/usr/bin/env python3
# Filename: whitelist.py

with open('whitelist.txt', 'r', encoding='utf-8') as f:
    whitelist = set(line.lower().rstrip() for line in f) 

while True:
    domain = input('Enter a domain or press return to exit: ') 
    if not domain: 
        break

    if domain.lower() in whitelist:
        print('Match on domain: ', domain)                
    else:   
        print('No match on: ', domain)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:58:56+00:00Added an answer on May 30, 2026 at 1:58 am
    for recd in whitelist: 
        # if input domain doesn't match record read next record (continue)
        if recd.lower().rstrip() != domain.lower():  
            continue
        else: # otherwise if match set match indicator
            match = 1
        break # after setting indicator stop reading records
    

    can be written more succintly

    for recd in whitelist:
        if recd.lower().rstrip() == domain.lower():
            match = 1
            break
    

    Also, you should be using True and False for booleans, rather than 0 and 1.

    Third, you should really be reading the whitelist file once, outside of the loop. Ideally, you’d read it into a set to allow for fast lookup.

    with open("whitelist.txt") as f:
        whitelist = set(ln.rstrip() for ln in f)
    

    Then the loop becomes

    while True:
        domain = input('Enter a domain or press return to exit: ')
        if not domain:
            break
    
        if domain in whitelist:
            print('Match on domain: ', domain)                
        else:   
            print('No match on: ', domain)
    

    Note that I’ve changed the loop condition to while True because the check for not domain already occurs inside it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.