【问题标题】:Why is my output wrong in this code?为什么我的输出在这段代码中是错误的?
【发布时间】:2016-02-14 23:44:10
【问题描述】:

我正在编写一个程序,根据用户输入显示有多少人使用特定的电子邮件提供商。我有一个 CSV 文件,每次运行程序时都会打印0

我的代码如下:

user = input('Enter an email'):
c=0
f_in = open('us-500.csv','r')
f_in.readline()
for line in f_in:
    line = line.strip(' ')
    first, last, company, address, city, country, state, zip, phone1, phone2, email, web = line.split(',')
    for count in email:
            if count == user:
                c +=1
print(c) 
f_in.close()

【问题讨论】:

  • 改为user = int(input('Enter an email:'))

标签: python csv python-3.x


【解决方案1】:

您似乎希望在各种电子邮件地址中匹配域 - @example.com。这是正确的吗?

如果是这样,您将不得不期望电子邮件看起来像一个典型的地址“user@example.com”,并首先将地址分解。

试试这个:

user, domain = email.split('@', 1)

此时,domain 将类似于“example.com”,这对您来说可能就足够了。

provider = input('Enter an email provider (like "gmail.com"): '):

count = 0
with open('us-500.csv', 'r') as users:
    users.readline()   # skip header lines
    for user in users:
        first, last, company, address, city, country, state, zip, phone1, phone2, email, web = user.split(',')
        _, domain = email.split('@', 1)
        if domain == provider:
            count += 1

print("Email provider '{}' has {} users".format(provider, count))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多