9.21 City Search (Part A, Reading a file) In this part, we are going to read data from a file and print its contents. In later parts, we will search through this data. A code template and some text files to test your code is already provided to you. Take a moment to look over them and download the text files. Investigate print_list The function print_list, is given to you. This function prints the contents of a list. It uses the enumerate function that you may not have seen before: experiment with it in Python Tutor using this starter code. Ask for help with any questions.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I need a better understanding on how to answer this question

1 def readfile(filename):
2
3
4
5
6
7
8
9
10
11
'''Reads the entire contents of a file into a list using the readline:
13
14
15
16
17
18
19
20
21
22 if
23
24
25
26
27
Args:
Returns:
list: Each element of this list is a line from the text in the fil
pass
filename (str): The name of the file to read.
def print_list(list_to_print):
"''Prints the contents of a list.
Args:
list to print (list): The list to print.
for i, item in enumerate(list_to_print):
print(i,':', item, sep="")
name
'__main__':
''Reads and prints a file's contents.
pass
Transcribed Image Text:1 def readfile(filename): 2 3 4 5 6 7 8 9 10 11 '''Reads the entire contents of a file into a list using the readline: 13 14 15 16 17 18 19 20 21 22 if 23 24 25 26 27 Args: Returns: list: Each element of this list is a line from the text in the fil pass filename (str): The name of the file to read. def print_list(list_to_print): "''Prints the contents of a list. Args: list to print (list): The list to print. for i, item in enumerate(list_to_print): print(i,':', item, sep="") name '__main__': ''Reads and prints a file's contents. pass
9.21 City Search (Part A, Reading a file)
In this part, we are going to read data from a file and print its contents. In later parts, we will search through this data.
A code template and some text files to test your code is already provided to you. Take a moment to look over them and download the text
files.
Investigate print_list
The function print_list, is given to you. This function prints the contents of a list. It uses the enumerate function that you may not have
seen before: experiment with it in PythonTutor using this starter code. Ask for help with any questions.
Implement readfile
Write the function readfile according the specification in its docstring. For example: using the file cities-small.txt, the function
readfile('cities-small.txt') returns the list
['Santa Rosa\n', 'Petaluma\n', 'Rohnert Park\n', 'Windsor\n', 'Healdsburg\n']
Note: Notice the trailing newline characters '\n' after each element in the list. We will remove them in the main function later. You do not
need to do anything to strip these out yet.
Complete main
For the main logic, replace pass with logic implementing the following:
• Prompt the user for the name of a file to read.
• Call the readfile function to retrieve the contents of that file, saving its return value (a list) in some variable (lets call it YourList).
• Remove the trailing newline characters from elements of your list by adapting the code below
YourList = [line.rstrip () for line in YourList]
• Where rstrip strips whitespace characters from the end of each line.
• This code employs a "list comprehension" pattern, discussed elsewhere in our book textbook.
Prints the number of lines in the file
Calls the print_list function to print the list (after you removed the trailing newline characters)
Sample output when the file provided is cities-small.txt
Number of lines in file: 5
0: Santa Rosa
1: Petaluma
2: Rohnert Park.
3: Windsor
4: Healdsburg
Transcribed Image Text:9.21 City Search (Part A, Reading a file) In this part, we are going to read data from a file and print its contents. In later parts, we will search through this data. A code template and some text files to test your code is already provided to you. Take a moment to look over them and download the text files. Investigate print_list The function print_list, is given to you. This function prints the contents of a list. It uses the enumerate function that you may not have seen before: experiment with it in PythonTutor using this starter code. Ask for help with any questions. Implement readfile Write the function readfile according the specification in its docstring. For example: using the file cities-small.txt, the function readfile('cities-small.txt') returns the list ['Santa Rosa\n', 'Petaluma\n', 'Rohnert Park\n', 'Windsor\n', 'Healdsburg\n'] Note: Notice the trailing newline characters '\n' after each element in the list. We will remove them in the main function later. You do not need to do anything to strip these out yet. Complete main For the main logic, replace pass with logic implementing the following: • Prompt the user for the name of a file to read. • Call the readfile function to retrieve the contents of that file, saving its return value (a list) in some variable (lets call it YourList). • Remove the trailing newline characters from elements of your list by adapting the code below YourList = [line.rstrip () for line in YourList] • Where rstrip strips whitespace characters from the end of each line. • This code employs a "list comprehension" pattern, discussed elsewhere in our book textbook. Prints the number of lines in the file Calls the print_list function to print the list (after you removed the trailing newline characters) Sample output when the file provided is cities-small.txt Number of lines in file: 5 0: Santa Rosa 1: Petaluma 2: Rohnert Park. 3: Windsor 4: Healdsburg
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education