Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the Output Into a File Finally, modify the filter_phone_calls function so that the output is written into the file indicated by the output_path argument

Write the Output Into a File

Finally, modify the filter_phone_calls function so that the output is written into the file indicated by the output_path argument instead of printed to the terminal. In order to implement this modification, you will need to have two files opened simultaneously - one that you read from (input_path) and one that you write to (output_path). It is perfectly ok to do something like this:

with open('file1', mode='r') as f1: with open('file2', mode='w') as f2: ... 

But it is more elegant to do the following:

with open('file1', mode='r') as f1, open('file2', mode='w') as f2: ... 

Be careful to use variables of different names for the two file objects (in the example above f1 and f2).

Congratulations, you have implemented the function that can be used to perform the requested phone calls log analysis.

How to Validate

Validate your work by placing the code below at the end of your task3.py file.

if __name__ == '__main__': filter_phone_calls( area_code=412, start_hour=0, end_hour=6, input_path='data/phone_calls.txt', output_path='data/phone_calls_filtered.txt' ) 

This time you should not see any output in the terminal. Instead, there should be a new 'data/phone_calls_filtered.txt' file. The file should contain the same output that you have previously observed in the terminal. There should only be the phone numbers with the 412 area codes and time stamps between midnight and 6 am in the morning.

image text in transcribed

image text in transcribed

Inspect the Handout Files There are two files used in thistask - task3.py and the data/phone_calls.txt data file. You will be making changes to the task3. py file only. You will not make any changes to the data/phone_cal.s. txt file. This is a data file your program will read, analyze, and report on. Note that the data/phone_calls.txt is a somewhat large file, and you may encounter some hiccups when trying to open it in your browser or common plain text editors, such as Notepad an Windows. In reality, datasets that you deal with may be much larger - typically from hundreds of MiB to hundreds of GiB and beyond. The good news is that Python can process the file line by line and, hence, allows you to perform the type of analysis you will be performing here on files of arbitrary size (provided you can fit them on your disk). Finally, real datasets often contain noise, such as encoding errors and malformed pieces. The file you will be using here is nice and clean. If you manage to open the file, which should not be a real problem on reasonably recent machines, you will observe that it cansists of 1,0o0,0o0 lines that look like the following; 2020-01-0100:00:09:+1(892)53292432020010100:00:57:+1(342)94442132020010100:02:08:+1(601)42161542020-01-0100:02:25:+1(671)977-39442020-01-0100:02:54:+1(901)77033052020010100:03:05:+1(761)8231060 There is a timestamp consisting of the date ( YYYY-MM-DD) and time (HH:MM: SS), followed by a phone number (+0(Ob0) b0b-0000). These two are separated by a colon and a space. Your task will be to select the lines that have the phone numbers with the 412 area code and timestamps between midnight (included), i.e., 00:00:00 and 6am (not included), i,e. 05:59:59. By inspecting the task3. py file you confirm that this time no scaffolding code is provided. You will have to write 100% of the code to solve the problem yourself. Iterate Over the File Line by Line As the first step, implement a function that reads the file and prints it to the terminal one line at a time. Start your work by creating the filter_phone_calls function with the following parameters: - area_code: An int indicating the focused area code. - start_hour:An int between 0 and 24 indicating the starting hour of the focused time span. - end_hour : An int between 0 and 24 indicating the finishing hour of the focused time span. - input_path: A str identifying the file from which the input should be read. - output_path:A str identifying the file to which the output should be written. You should see a similar output as before. However, only the calls that happened between midnight and 6 am should be listed. Write the Output Into a File Finally, modify the filter_phone_calls function so that the output is written into the file indicated by the output_path argument instead of printed to the terminal. In order to implement this modification, you will need to have two files opened simultaneously - one that you read from (input_path ) and one that you write to (output_path ). It is perfectly ok to do something like this: with open('filel', mode =r) as f1 : with open( "file2', node=' w ') as f2 : But it is more elegant to do the following: with open('file1", mode=' r) as f1, open( file2, mode=" w ') as f2 : .. Be careful to use variables of different names for the two file objects (in the example above f1 and f2 ). Congratulations, you have implemented the function that can be used to perform the requested phone calls log analysis. How to validate Validate your work by placing the code below at the end of your task 3 .py file. This time you should not see any output in the terminal. Instead, there should be a new 'data/phone_calls_filtered.txt' file. The file should contain the same output that you have previously observed in the terminal. There should only be the phone numbers with the 412 area codes and time stamps between midnight and 6am in the morning. Assessment

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago