简体   繁体   中英

How can I make this code shorter but achieve more - Python - Email Validation Program

I am trying to create a program which validates an email, to check whether it is valid or not. One of my functions is to find out if there is a quote in the email, then separate the quote from the local section in the email and keep separating it until all the quotes are put into separate variables. What I have done so far is this:

local = help."hello"."a"
quote_list = local.split('"')
            print(quote_list)
            leng = len(quote_list)
            print(leng)

This code above is just to help the code below work properly
This is the part that I need help with (see below):

            if leng == 3:
                quote = quote_list[1]
            if leng == 5:
                quote, quote_2 = quote_list[1], quote_list[3]
            if leng == 7:
                quote, quote_2, quote_3 = quote_list[3], quote_list[5], quote_list[3]

Is there any way to make the code shorter, and repeat for a longer number of times? Many Thanks Hope you guys can find a way to fix this problem

How about ...

quotes = [
    quote_list[i] for i in range(1, leng)
]

This will walk through your quote_list and pick up every second element starting at index 1 (= the second element).

Then you can process these quotes (now stored in quotes ) in a next step.


But have in mind that this won't work so easily in general. As soon as so forgets to close a single quote things will go wrong. (And so will forget a quote at some time.) Additionally some email tools might use different kinds of quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM