Question

PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may...

PYTHON : Create a Email Address Parser (* Please do make comments*)

Often times, you may be given a list of raw email addresses and be asked to generate meaningful information from such a list. This project involves parsing such a list and generating names and summary information from that list.

The script, eparser.py, should:

  1. Open the file specified as the first argument to the script (see below)
  2. Read the file one line at a time (i.e., for line in file–each line will be a separate email address)
  3. Print (to the screen) a line that “interprets” the email address as described below
  4. When finished, display the total number of email addresses and a count unique domain names

Each email address will either be in the form “[email protected]” OR “[email protected]”, where “first” and “last” are the first and last names of the person respectively, and “i” is the first initial. Each “line” (step 4) should contain the email address itself, the name (either “Last, First” or “Last, I.” where “I” is the first initial, and the domain name of th email address. The name components should be in “title case” (capitalize first letter of all words/initials). So for example, the following two email addresses in the input file:

[email protected] 
[email protected] 
[email protected] 
[email protected]

should result in the following printed to the screen:

[email protected]                    Deer, Doe                ray.me 
[email protected]                 Robot, I.                sneaker.net 
[email protected]                     Sew, Far                 ray.me 
[email protected]                        Tea, L.                  ray.me 
Number of Addresses: 4
Unique Domain Names: 2

Allow 35 characters for the email address, 25 characters for the full name and 20 characters for the domain name for a total of 80 characters wide per line of output. All fields should be left aligned (that’s the default for strings).

HINTS

  1. The “arguments” to a python program are in the sys.argv list. The first element in the list is the name of the script and the rest are any additional words (arguments) added to the end. See here for explanation. The code needed may look as follows:
import sys

filename = sys.argv[1]

REQUIRED IMPLEMENTATION NOTES

  1. You MUST abstract your most useful logic by creating a function named parse_address that takes an email address as a parameter and prints the line formatted as described above. This function should be “called” as your step 3 above, which will make the main script much cleaner. So to reiterate, the following function MUST be in your code:
        def parse_address(email): 
            ... DO STUFF HERE ...
  1. You MUST use either the format() function or an f-string to format your lines of text. In other words, you cannot use the center(), ljust() and rjust() methods. Those methods are okay, but don’t use them here.

  2. You MUST run your script against the file emails.txt and redirect the output to contacts.txt. In other words, you should run python eparser.py emails.txt > contacts.txt. This file should be in your final submission. DO NOT WRITE TO A FILE INSIDE OF YOUR SCRIPT. ONLY READ AND PRINT. WRITING OCCURS HERE BY REDIRECTING OUTPUT.

OPTIONAL CHALLENGES( IF YOU CAN DO THEM THATS FINE IF YOU CANNOT ITS ALRIGHT)

  1. Create a second version of the script, eparser2.py. This version should import the parse_address function from eparser (from eparser import parse_address). Instead of asking for a file name and reading the text from the file, instead read in the text line-by-line from sys.stdin. This should allow you to run it interactively AND to pipe in text from another command (e.g., type emails.txt | python eparser2.py).

  2. Improve the “logic” for parse_address to remove any numbers (digits) from the name before running your “rules.” So for instance, [email protected] would parse to a name of “Cunningham, D.” (dropping the “12”).

  3. Improve the logic of parse_address to ALSO capitalize the third letter of any name that starts with Mc. So for instance Mcshady whould become McShady.  

BELOW WILL BE THE FILES REQUIRED FOR THIS TO WORK:

emails.txt FILE will have:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

sample_contacts.txt FILE:

[email protected] Smith, A. boring.com
[email protected] Witcher, A. coldasheck.net
[email protected] Johnson, Demarcus semper.org
[email protected] Baker, Shelly supplyshack.com   
[email protected] Parker, P. spider.org
[email protected] Markle, Meghan royals.gov
[email protected] Mcshady, Seedy criminal.inc
[email protected] Bird, B. sesame.st   

Number of Addresses: 8
Unique Domain Names: 7


Homework Answers

Answer #1

ANSWER:-

GIVEN:-

out=open("contacts.txt","w")#open file in write mode
domain_list=[]
def parse_address(email):#method that writes the output file the last, first name and domain
temp=[]
temp=email.split("@")
if '.' in temp[0]:
out.write(l[i]+" "+temp[0].split('.')[0].title()+","+temp[0].split('.')[1].title()+" "+temp[1]+"\n")
domain_list.append(temp[1])
else:
out.write(l[i]+" "+temp[0][1:].title()+","+temp[0][0].title()+" "+temp[1]+"\n")
domain_list.append(temp[1])
  
f=open("emails.txt","r")#open file in read mode
l=f.readlines()
for i in range(len(l)):
l[i]=l[i][:-1]
total_emails=0
domain_names_count=0

for i in range(len(l)):
parse_address(l[i])#call function for each email
  
domain_set=list(set(domain_list))
print("The total emails:"+str(len(l)))
print("The count of unique domain names:"+str(len(domain_set)))
out.write("The total emails:"+str(len(l))+"\n")
out.write("The count of unique domain names:"+str(len(domain_set))+"\n")
out.close()

SCREEN SHOTS:-

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
Write regular expressions that will match IP addresses and usernames in a Microsoft IIS log file....
Write regular expressions that will match IP addresses and usernames in a Microsoft IIS log file. First, you will write a regular expression that matches IP addresses within the re.compile function call on line 36. Next, you will write a regular expression that matches usernames (in the format domain\username) on line 50. Finally, in steps 3 and 4 you will write a loop for each step that displays all the IP addresses (gathered into the list ipAddresses) and usernames (gathered...
Please create a python module named homework.py and implement the functions outlined below. Below you will...
Please create a python module named homework.py and implement the functions outlined below. Below you will find an explanation for each function you need to implement. When you are done please upload the file homework.py to Grader Than. Please get started as soon as possible on this assignment. This assignment has many problems, it may take you longer than normal to complete this assignment. This assignment is supposed to test you on your understanding of reading and writing to a...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
Create a new NetBeans project (using Ant) titled YourNameExam2P3. 2. Pretend you are a malware author...
Create a new NetBeans project (using Ant) titled YourNameExam2P3. 2. Pretend you are a malware author and you are trying to come up with random-looking web domain names for your command-and-control viruses, e.g. “RDf-45dlfjxg7.com”. 3. First, you need to come up with some original criteria that define your domain name pattern. For example, “it should be 13 characters long, include 3 integers only, no lower case letters, and possibly end in .net or .edu”. You should come up with your...
WRITE USING PYTHON PROGRAMMING THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED....
WRITE USING PYTHON PROGRAMMING THE CODE GIVEN BELOW HAS SOME ERRORS WHICH NEED TO BE SOLVED. ALSO THE 2 POINTS MENTIONED BELOW SHOULD BE PRESENT IN THE CODE Write a script that calculates the 3 longest words of a text stored in a file and print them from the longest to the smaller of the 3. Please note: 1. The name of the file is word_list.csv and it doesn’t need to be asked to the user (meaning the name will...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...
In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text: To be or not to be, that is the...
For this assignment, you need to submit a Python program that gathers the following employee information...
For this assignment, you need to submit a Python program that gathers the following employee information according to the rules provided: Employee ID (this is required, and must be a number that is 7 or less digits long) Employee Name (this is required, and must be comprised of primarily upper and lower case letters. Spaces, the ' and - character are all allowed as well. Employee Email Address (this is required, and must be comprised of primarily of alphanumeric characters....
PYTHON Driver’s License The local driver’s license office has asked you to create an application that...
PYTHON Driver’s License The local driver’s license office has asked you to create an application that grades the written portion of the driver’s license. The paper has 20 multiple-choice questions. Here are the correct answers: A C A A D B C A C B A D C A D C B B D A Your program should store these correct answers in a list. The program should read the student’s answers for each of the 20 questions from a...
Part 1 - LIST Create an unsorted LIST class ( You should already have this code...
Part 1 - LIST Create an unsorted LIST class ( You should already have this code from a prior assignment ). Each list should be able to store 100 names. Part 2 - Create a Class ArrayListClass It will contain an array of 27 "list" classes. Next, create a Class in which is composed a array of 27 list classes. Ignore index 0... Indexes 1...26 correspond to the first letter of a Last name. Again - ignore index 0. index...