简体   繁体   English

如何使此代码更短但实现更多 - Python - 电子邮件验证程序

[英]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).这将遍历您的quote_list并从索引 1(= 第二个元素)开始选取每个第二个元素。

Then you can process these quotes (now stored in quotes ) in a next step.然后您可以在下一步中处理这些引号(现在存储在quotes )。


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. (因此有时忘记引用。)此外,一些电子邮件工具可能会使用不同类型的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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