CSc 250 Lab 7

In this lab, you will be tasked with writing several functions that do various tasks with file reading and writing. You should create one file named lab7.py and put all of the functions you complete, along with the test-cases, in the same file. Submit just this file to D2L when you are finished.

Problem 1 (warm-up)

Write a function named print_first_words. This function will take one parameter - the name of the file to read. This function will open the file, read in the data, and print out the first word on each line to the python console, all on the same line. For example, if there is a file named input1.txt in the same directory as lab7.py with these contents:
the craziest thing
cloudiest buy website
day is lighter than night
ever seen that movie?
Then running:
print_first_words('input1.txt')
Should print out:
the cloudiest day ever

Problem 2

Write a function named reverse_file. This function will take one parameter - the name of the file to read. This function will open the file, read in all of the lines, reverse the order of the lines, and then write over the original file with the reversed content. For example, if there is a file named input1.txt in the same directory as lab7.py with the same contents as the previous problem, then calling the funciton like so::
print_first_words('input1.txt')
Should change the contents of the input file to be:
ever seen that movie?
day is lighter than night
cloudiest buy website
the craziest thing

Problem 3

Write a function named cap_word_count. This function will take one parameter - the name of the file to read. This function will return a single integer, which should be the number of capitalized words (words that begin with a capitalized letter) in the file. For example, if there is a file named input2.txt in the same directory as lab7.py with the these contents:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, Sed Do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex Ea Commodo consequat.
Duis aute irure Dolor In reprehenderit in voluptate velit Esse cillum dolore eu fugiat Nulla pariatur.
Excepteur sint Occaecat Cupidatat non proident, Sunt in culpa qui officia deserunt mollit anim id est laborum.
And then this test is run:
file_name = 'input2.txt'
print('Capitalized word count in ' + file_name + ' is ' + str(cap_word_count(file_name)))
Ths test should print out:
Capitalized word count in input2.txt is 15

Problem 4

Write a function named grab_csv_column. This function will take two parameters. The first is the name of a CSV file to read, and the second is an integer representing a column number. This function will read in the CSV file, and extract all of the entries of the CSV file that line up with the column number specified, in sorted order. It should also ignore the first line (the line with the column labels). For example, if there is a file named data.csv in the same directory as lab7.py with the these contents:
Name,SID,exam1,exam2,final
Steve,1429,67,84,91
Annie,3211,81,85,85
James,3171,90,75,80
Carlita,7877,85,87,89
And then this test is run:
print_csv_column('data.csv', 1)
print('---')
print_csv_column('data.csv', 3)
Ths test should print out:
Annie
Carlita
James
Steve
---
67
81
85
90

You must turn in at least 3 of the problems on D2L to get credit for this.

Solutions: solutions.zip