Question

In: Computer Science

Specification This script reads in class data from a file and creates a dictionary. It then...

Specification

This script reads in class data from a file and creates a dictionary.

It then prints the data from the dictionary.

The script has the following functions

  • class_tuple_create
  • print_classes

class_tuple_create

This function has the following header

def class_tuple_create(filename):

The function reads in a file with four fields of data about a class.

The fields are

        
Course ID       Room number     Instructor      Start time

The function must print an error message if the file cannot be opened for any reason.

The function should read in each line of the file and split it into a list of 4 elements.

It should add the information in the line into a dictionary, where the key is the course number and the value is a tuple consisting of the room number, the instructor and the start time.

The function should return this dictionary when it has read all the lines in the file.

print_classes

This function has the following header

def print_classes(class_info):

It should print the following table header

ID      Room            Instr   Start

Then it should print out the data for each class in alphabetical order by Class ID.

The data for each course should align with the labels.

Test Code

The script must contain the following test code at the bottom of the file

class_information = class_tuple_create('xxxxx')
class_information = class_tuple_create('courses.txt')
print_classes(class_information)

For the test code to work you must copy into your hw3 directory the file from /home/ghoffman/course_files/it117_files.

cp  /home/ghoffman/course_files/it117_files/courses.txt  .

Suggestions

Write this script in stages, testing your script at each step

  1. Create the file hw3.py and copy the function header for class_tuple_create into it.
    For the body of this function enter pass. Copy the function header for print_classes into the script.
    Again use pass as the function body.
    Copy the test code above into the script.
    Make the script executable and run it.
    You should see nothing.
    If you get an error message, fix the problem before proceeding.
  2. Remove the pass statement in class_tuple_create.
    Create a file object so you can read the file given as a parameter.
    Write an error message if the file cannot be opened.
    Run the script.
    You should see one error message.
  3. Using a for loop print each line of the file.
    Run the script and fix any errors.
  4. Remove the print statement you just created.
    In it's place use the split string method to create a list of words in the line.
    Print the list of words.
    Run the script and fix any errors.
  5. Remove the print statement you just created.
    Define the variable course_id and set its value to the first element of the list.
    Print the value of this variable.
    Run the script and fix any errors.
  6. Create the variable class_data and set its value to a tuple consisting of the remaining fields in the list.
    You might want to use a slice to do this and tuple conversion function to do this.
    Print course_id and class_data.
    Run the script and fix any errors.
  7. Create an empty dictionary at the top of the function.
    Inside the for loop add an entry to the dictionary where the key is the class ID and the value is the tuple.
    Outside the loop but still inside the function, print this dictionary.
    Run the script and fix any errors.
  8. Remove all print statements from class_tuple_create.
    Have the function return the dictionary you just created.
    Remove the pass statement from print_classes and replace it with a statement that prints the parameter.
    Run the script and fix any errors.
  9. Using a for loop, print the class IDs in sorted alphabetical order.
    Run the script and fix any errors.
  10. Inside the loop use the create the variable course_data and assign it the tuple associated with each class ID.
    Print the class ID and tuple for each class.
    Run the script and fix any errors.
  11. Change the print statement so that it prints the class ID and each element of the tuple with a Tab between each value.
    Run the script and fix any errors.
  12. Print the labels for each column and a line of dashes underneath it.

Output

The output should look something like this

Error: Unable to open xxxxx
ID  Room    Instr   Start
-------------------------------
CM241   1411    Lee 13:00
CS101   3004    Haynes  8:00
CS102   4501    Smith   9:00
CS103   6755    Rich    10:00
NT110   1244    Burke   11:00

Solutions

Expert Solution

#--------- hw3.py ----------------

#function that reads a file and returns

#dictionary of courses with courses information in file

def class_tuple_create(filename):

try:

#using with open the file

with open(filename,"r") as f:

#empty dictionary

class_info = {}

#for each line in f

for line in f:

#split the line by whitespace(if you don't passs any parameters)

#it will split by whitespace.

data = line.strip().split()

#if length of data is not 4 don't load that course info.

if(len(data)!=4):

print("Invalid Number of fields in line:",line.strip())

else:

#store the course_id

course_id = data[0].upper()

#store the rest of the tuple

course_data = tuple(data[1:])

#insert into dictinoary

class_info[course_id] = course_data

#return the dictionary

return class_info

except Exception as e:

#if any exception occured return None.

print("Error: Unable to open",filename)

return None

#function to print class info dictionar

def print_classes(class_info):

#if passed dictionary is Not None

if(class_info != None):

#to print the header with some allignment use below >8 means right allign by 8 spaces

print("\n{:<8}\t{:>8}\t{:>8}\t{:>8}".format("ID","Room","Instr","Start"))

#for each key in dictionary

for key in class_info:

#using allignment > or <(left allignment) format the output to print the

#values below the headers.

print("{:<8}\t{:>8}\t{:>8}\t{:>8}".format(key,*class_info[key]))

#testing.

class_information = class_tuple_create('xxxxx')

class_information = class_tuple_create('courses.txt')

print_classes(class_information)


Related Solutions

In this assignment you will write a PHP program that reads from a data file, creates...
In this assignment you will write a PHP program that reads from a data file, creates an associative array, sorts the data by key and displays the information in an HTML table. Create a PHP file called hw4.php that will do the following: - Display your name. - Read text data from a file. The data file is hw3.txt. The file hw3.txt will hold the following text: PQRParrot, Quagga, Raccoon DEFDo statements, Else statements, For statements GHIGeese, Hippos, If statements...
Implement in Python a script that does the following: 1) reads input from a supposed file...
Implement in Python a script that does the following: 1) reads input from a supposed file called firstnames_2.txt. 2) processes the input and writes and saves the output to a file. NOTE: Please make sure that the names are written in the outfile with one name on each line no comma ( , ) after the name in the output
For this problem, you'll be writing a script that reads the content of a file, modifies...
For this problem, you'll be writing a script that reads the content of a file, modifies that content, and then writes the result to a new file. a. Define a function called modified() that takes a string and returns a "modified" version of the string. The type of modification is up to you (be creative!), but I have a couple requirements: You must make at least two different modifications, and at least one of those modifications must be some kind...
Write a program in a script file that creates m × n matrix with elements that...
Write a program in a script file that creates m × n matrix with elements that have the following values. The value of each element in the last row is the number of the column. The value of each element in the last column is the number of row +1. For the rest of the elements, each has a value equal to the sum of the element below it and the element to the right.
Create a python script that reads in the contents of CityPop.csv and stores the data in...
Create a python script that reads in the contents of CityPop.csv and stores the data in a container. Task 2 will expect that your program can find cities by name within the container, so think about that as you set up your data container. Briefly describe how you store the data in comments. *It won't let me post the CSV file
Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
Write a script named numberlines.py. This script creates a program listing from a source program. This...
Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns, so that the...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list...
Solve using PYTHON PROGRAMMING 9. Write a script that reads a file “ai_trends.txt”, into a list of words, eliminates from the list of words the words in the file “stopwords_en.txt” and then a. Calculates the average occurrence of the words. Occurrence is the number of times a word is appearing in the text b. Calculates the longest word c. Calculates the average word length. This is based on the unique words: each word counts as one d. Create a bar...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure...
Solve using PYTHON PROGRAMMING Write a script that reads a file “cars.csv”, into a pandas structure and then print a. the first 3 rows and the last 3 of the dataset b. the 3 cars with the lowest average-mileage c. the 3 cars with the highest average-mileage. Solve using PYTHON PROGRAMMING
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT