Consider the addressbook file provided as part of this lab. It has 3 field: emai
ID: 3784286 • Letter: C
Question
Consider the addressbook file provided as part of this lab.
It has 3 field: email address, last name, and first name.
The structure of email addresses is as followed: username@imapNumber.asu.edu
You are to reformat the addressbook as followed:
LastName FirstName Username EmailAddress
The requirements/observations for our targeted format:
Notice that there is a new field, the username.
The field separator for the final display is the tab. Note that the initial file did not have a uniform field separator
The data must be sorted by last name
The imap number must be removed from the email addresses. For example, “madhu@imap2.asu.edu” must now be shown as madhu@asu.edu
You’ll need to use the best possible ways (commands, pipelines, sequence) that result in an efficient processing (i.e. fewer keystrokes).
Explanation / Answer
fin = open("inputfile.txt","r")
finl =f.readlines()
fout = ("outputfile.txt", "w")
for x in finl:
words = x.split()
uname = words[2].split("@")
domain = uname[1].split('.', 1)[-1]
email = uname[0] + domain
fout.write('words[1] words[0] uname[0] email ')
fin.close()
fout.close()
EXPLANATION AND DEMO:
Let the inputfile.txt contains following line:
Singh Jaskaran singh.jaskaran@1234.asu.edu
then
x = Singh Jaskaran singh.jaskaran@1234.asu.edu
words = [Singh, Jaskaran, singh.jaskaran@1234.asu.edu] // split x at space
words[1] -- Firstname -- Jaskaran
words[0] -- Lastname -- Singh
words[2] -- process in next line
uname = [singh.jaskaran, 1234.asu.edu] // split words[2] at '@'
uname[0] -- Username -- singh.jaskaran
domain = asu.edu // trim the character upto and inluding first '.'
email = singh.jaskaran93@asu.edu // concatenate username and domain
email -- email -- singh.jaskaran93@asu.edu
Write in outputfile.txt as follow:
words[1] words[0] uname[0] email // -- tab and -- newline
Result
Jaskaran Singh singh.jaskaran93 singh.jaskaran93@asu.edu