[使用 Python 3.x] 我正在尝试创建一个包含两列的 CSV 文件,一列包含假电子邮件地址,第二列应包含相应函数中指定的某个国家/地区代码。
我希望国家代码 - 至少 - 统一分配给每个电子邮件地址。但是,如果还有一种方法可以使它们不均匀分布,那就太好了。例如,一个国家可以分配 30% 的电子邮件地址,另一个国家分配 10% 的电子邮件地址,等等。
我最大的困难是创建一个字典,其中键是电子邮件地址,值是国家代码,从而压缩两个长度不等且没有空值(无)的列表。附带说明一下,我认为创建字典是最好的方法,但我对编程和 python 很陌生,因此如果你有更好的解决方案,请分享!
这是我的代码:
from random import choice, randint
from string import ascii_lowercase
from itertools import zip_longest
import csv
def gen_name(length):
""""Generates a random name with the given amount of characters."""
return ''.join(choice(ascii_lowercase) for i in range(length))
def email_gen():
"""Generates a fake email address."""
user = gen_name(randint(5, 10))
host = gen_name(randint(5, 15))
return user + "@" + host + ".com"
def gen_plain_email_list(n):
"""Generates a list of n amount of random e-mail addresses"""
emaillist = []
for i in range(n):
emaillist.append(email_gen())
return emaillist
def gen_email_dict(n):
"""Generates a dictionary where the key is an e-mail address and the value a random country code."""
email_list = []
cc = ['us', 'gb', 'de', 'fr', 'it', 'nl', 'es', 'ae', 'br', 'au']
# Creating a list of n amount of e-mail addresses
for i in range(n):
email_list.append(email_gen())
# Creates dictionary with with an e-mail address from email_list and
# a random country code from the cc list
email_dict = dict(zip_longest(email_list, cc, fillvalue=choice(cc)))
return email_dict
def dict_to_csv(filename, n):
with open(filename, 'w', newline='') as f:
w = csv.writer(f)
w.writerows(gen_email_dict(n).items())
dict_to_csv('test.csv', 1000)
在此先感谢您的帮助!