写一个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:
[email protected]
[email protected]
[email protected]
[email protected]
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):
写一个Python函数,读取文件并返回一个字典,它的键是电子邮件的用户名和他们的键是电子邮件的域。_第1张图片
写一个Python函数,读取文件并返回一个字典,它的键是电子邮件的用户名和他们的键是电子邮件的域。_第2张图片

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 [email protected]
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)}')

你可能感兴趣的:(python,python,开发语言,信息可视化)