写一个Python函数,读取文件并返回一个字典,它的键是电子邮件的用户名和他们的键是电子邮件的域。

Question 4
Not yet
answered
Marked out of
15.00
Flag question
Note: lf you use functions which have not been taught in class, you will getzero for this question no matter whether the code is correct or not.
Assume that you have a file that its contents is a list of emails like below:
Smitjohn@gmail.com
greg.pearson@gmail.com
abolfazlbabaei@yahoo.com
Anne.parker@hotmail.com
Write a Python function that read the file and return a dictionary that its keys are the usernames of the emails and theirkeys are the domain of the email. For example, the output of the desired function for an input like above should be.
(‘smit;johhn’: ‘gmail.com’, ‘anne,parker’: "hotmail.com’, "abolfazlbabaei’ ‘yahoo.com’, ‘greg,pearson’: ‘gmail.com’
To answer this question, complete the function below:
def emailDomain(fileName):
Hint: ln order to answer the first part of the guestion, you have to read the file and split the contents based on "@’ andadd each user name and domain of the emails to key and values of a dictionary respectively.Then, generate a password for each email separately. The password must include the first two and the last twocharacters of each email username. The location of these characters are up to you.
To generate the password, you must ask the user to enter the length of password, and then you must generate thepassword for each email separately. The password must include a mixture of upper- and lower-case letters. Use thefunction below to generate the password:
def Password generation(Username of email):
在这里插入图片描述
在这里插入图片描述

def emailDomain(fileName):
Hint: In order to answer the first part of the guestion, vou have to read the file and split the contents based on "@’ andadd each user name and domain of the emails to key and values of a dictionary respectively.Then, generate a password for each email separately. The password must include the first two and the last twocharacters of each email username. The location of these characters are up to you.lo generate the password, you must ask the user to enter the length of password, and then you must generate thepassword for each email separately. The password must include a mixture of upper- and lower-case letters. Use thefunction below to generate the password:
def Password generation(Userame of email):
Example:
For abolfazl.babaei@yahoo.com
If the length of password is 16
The password can be:
Ljkbhghabjuhueih

import random
import string

charts = string.ascii_uppercase + string.ascii_lowercase


def emailDomain(fileName):
    _result = {}
    with open(fileName, 'r', encoding='utf-8') as f:
        for row in f.readlines():
            if not row.strip():
                continue
            _tmp = row.strip().split('@')
            _result[_tmp[0]] = _tmp[1]
    return _result


def Password_generation(Username_of_email, length):
    _first_word = Username_of_email[:2]
    _latest_word = Username_of_email[-2:]
    _tmp_charts = [_first_word, _latest_word]
    for i in range(length - 4):
        _tmp_charts.append(random.choice(charts))
    random.shuffle(_tmp_charts)
    return ''.join(_tmp_charts)


if __name__ == '__main__':
    all_users = emailDomain('test2.txt')
    password_length = int(input('Please enter the password length: '))
    if password_length <= 4:
        print('Password length must be greater than 4')
    else:
        for user, domain in all_users.items():
            print(f'User: {user}, password: {Password_generation(user, password_length)}')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值