【问题标题】:Split text before @ in email in Django在Django中的电子邮件中在@之前拆分文本
【发布时间】:2013-10-11 03:03:41
【问题描述】:

您好,我的视图中有一个数组,例如

user_domain_array = ['rohit@ghrix.com','rahul@ghrix.com','rishi@ghrix.com']

我需要删除每个元素 before @ 从所有电子邮件 ID 中的文本,以便我可以获得 ,例如

ghrix.com

请建议我该怎么做。

【问题讨论】:

  • 向我们展示您的代码并告诉您问题出在哪里,我们不会为您编写代码。

标签: python django string split views


【解决方案1】:

正则表达式 = re.compile("@")

[ i.split(m.group())[1] for i in user_domain_array for m in [regex.search(str(i))] if m]

它可以在两种输入条件下工作-:

1) user_domain_array = ['rohit@ghrix.com','rahul@ghrix.com','rishi@ghrix.com']

2) user_domain_array = ['rohit.ghrix.com','rahul@ghrix.com','ghrix.com']

【讨论】:

    【解决方案2】:

    使用str.split():

    for email in user_domain_array:
        domain = email.split('@', 1)[-1]
    

    这会在@ 上拆分字符串一次,然后获取结果列表的最后一个元素。这样做的好处是即使输入字符串中没有 at 符号,您仍然可以获得结果。

    演示:

    >>> 'rohit@ghrix.com'.split('@', 1)[-1]
    'ghrix.com'
    >>> 'rohit.ghrix.com'.split('@', 1)[-1]
    'rohit.ghrix.com'
    

    【讨论】:

    • @sachitad:也许是因为我敢于回答一个在投票者看来不够努力的问题?
    • 哦,可能是太嫉妒你的好名声了! :)
    【解决方案3】:
    >>> user_domain_array = ['rohit@ghrix.com','rahul@ghrix.com','rishi@ghrix.com']
    >>> [domain.split('@')[1] for domain in user_domain_array]
    ['ghrix.com', 'ghrix.com', 'ghrix.com']
    >>> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 2021-10-17
      • 2018-12-17
      • 1970-01-01
      • 2016-07-27
      • 1970-01-01
      相关资源
      最近更新 更多