Question

please type code in Python find all the VALID email address in this input text file...

please type code in Python

find all the VALID email address in this input text file and store them as a single column of valid email addresses in an output text file labeled OutputFile

First cut/paste the block of text below into a separate text file and label that data file InputFile:

FinancialAid Email: [email protected]
Scholarships Financial Aid Phone: 979-555-3236
Email: [email protected] Financial [email protected] Aid
[email protected] Help Desk Central
[email protected] Dean's Office     
Jill Educational Program Coordinator II   


Create a program will find all the email address in InputFile and put into an output data file that you will label as OutputFile
Hint: use the character "@" as a cue to help find all the potential email addresses and then
see if you see an ".edu" or a ".net" or a...etc

You will want to check for valid email addresses ie ending in a valid extension. If you find the @ but when you test the rest of the candidate email, you don't see a valid extension simply flag it as an "ERROR" as shown in the output you should get:

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

f = open('inputFile.txt')
fo = open("outputFile.txt", "w")
line = f.readline()
while line:
extension=["edu","net","org","com","msv"]
lines=line.split(" ")
for word in lines:
if '@' in word:
dot=word.find(".")
if(dot>0):
ext=word[dot+1:]
while(True):
dot=ext.find(".")
if(dot>0):
ext=ext[dot+1:]
else:
break
ext=ext.rstrip()
if ext in extension:
word=word.rstrip()
fo.write(word+"\n")
else:
fo.write("Error"+word+"\n")
else:
fo.write("Error "+word+"\n")
line = f.readline()
f.close()
fo.close()

inputFile.txt

outputFile.txt

codeSnaps

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Answer #2

One possible solution would be to use regular expressions to search for patterns that match email addresses. Here is an example of how to do this using Python:

pythonCopy codeimport re# Open the input file and read its contentswith open('InputFile.txt', 'r') as f:
    input_text = f.read()# Define a regular expression pattern to match email addressespattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'# Find all the matches in the input textmatches = re.findall(pattern, input_text)# Filter out any invalid email addressesvalid_emails = [email for email in matches if email.endswith(('.com', '.edu', '.net', '.org'))]# Write the valid email addresses to the output filewith open('OutputFile.txt', 'w') as f:
    f.write('\n'.join(valid_emails))

This code reads the input file into a string, defines a regular expression pattern to match email addresses, and uses the re.findall() function to find all the matches in the input text. It then filters out any invalid email addresses by checking that the email address ends with a valid extension (.com, .edu, .net, or .org). Finally, it writes the valid email addresses to the output file.


answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
please type code in Python find all the VALID email address in this input text file...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT