【问题标题】:Regex value not matching with email IDs from CSV正则表达式值与 CSV 中的电子邮件 ID 不匹配
【发布时间】:2019-02-13 07:58:43
【问题描述】:

我正在尝试对 CSV 文件中存在的电子邮件 ID 进行基本检查。我不知道为什么它没有通过“if”检查。

import csv
import re
input_file = open("test_list.csv", "r").readlines()
print(len(input_file))
csv_reader = csv.reader(input_file)
line_count = 0
try:
    for row in csv_reader:
        line_count += 1
        print('Checking ' + str(line_count) + ' of ' + str(len(input_file)))
        name = {row[0]}
        email = list({row[2]})
        print(str(email[0]))
        print('Checking contact name'+str(name))
        regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
        match = re.match(regex,str(email[0]))
        if match == None :
            print("Bad Email")
        else:
            print("Good Email") 
        print('')
        print('')
except IndexError as error:
    print('Checked all the data')

我的 csv 文件是这样的:

bhanu1, singh2, bha.nu@gmail.com
bhanu2, singh2, bhadoxit.com
bhanu3, singh2, bhan@esnotexit.com

我的输出是:

3
Checking 1 of 3
 bha.nu@gmail.com
Checking contact nameset(['bhanu1'])
Bad Email

Checking 2 of 3
 bhadoxit.com
Checking contact nameset(['bhanu2'])
Bad Email

Checking 3 of 3
 bhan@esnotexit.com
Checking contact nameset(['bhanu3'])
Bad Email

【问题讨论】:

  • 你的缩进是错误的。你应该缩进for循环的代码,否则循环是空的
  • 如果破折号“-”可以是电子邮件的一部分,它应该在方括号中转义。像这样:[a-z0-9\-]
  • @Gnudiff 不,破折号可以放在字符类的最后而不转义。
  • 问题是您在每封电子邮件之前都有一个空格,您可以在输出中看到它。所以你应该在你的匹配中添加一个strip()re.match(regex,str(email[0]).strip()) 它应该可以工作

标签: python regex csv


【解决方案1】:

您的所有电子邮件地址都以空格开头,因为您没有修剪相邻的空格。

此外,您的代码有大量非常奇怪和迂回的方式来操作您的数据。这是使用内联 cmets 进行的重构。

import csv
import re

input_file = open("test_list.csv", "r").readlines()
print(len(input_file))

csv_reader = csv.reader(input_file)
# Compile regex once, use multiple times inside loop
regex = re.compile(
    r'^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$')
try:
    for line_count, row in enumerate(csv_reader, 1):
        print('Checking {0} of {1}'.format(line_count, len(input_file)))
        # Don't make a set out of this
        name = row[0]
        # Don't make a list out of this; trim spaces
        email = row[2].strip()
        print(email)
        print('Checking contact name {}'.format(name))
        match = regex.match(email)
        if match is None:
            print("Bad Email")
        else:
            print("Good Email") 
        print('')
except IndexError as error:
    print('Checked all the data')

try/except 处理仍然很奇怪,将文件读入内存并然后将其作为 CSV 读取是相当笨拙的。

【讨论】:

  • 您好,感谢您完善我的代码。你对处理有什么建议?我对这些东西很陌生。提出解决方案,我很乐意将其合并到我的代码中。
  • 如果您不需要提前知道有多少行,您可以打开文件并开始循环。 try/except 目前似乎没有做任何有用的事情,所以也许把它拿出来
  • 如果我不使用 try catch 它会给我一个 IndexError 超出范围。我该怎么办?
  • 我的猜测是你的 csv 中有一些行,它们要么是空的,要么没有 3 个值。然后您对row[0]row[2] 的访问会产生错误,您可以在循环开始时检查行的长度,如果不是3,您可能想跳过csv 的那一行
  • 我无法使用您发布的示例数据重现 IndexError。捕获错误并让程序声称它处理了所有数据,而错误特别导致它不处理所有数据似乎无论如何都具有误导性。
【解决方案2】:

在您的输出中,您可以在电子邮件前面看到一个空格。如果您删除它,它应该可以正常工作。只需将strip() 添加到您的代码中即可进行匹配。

match = re.match(regex,str(email[0]).strip())

【讨论】:

  • 非常感谢!我错过了那个小空间。它让我拉扯我的头发。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多