Contents
- avgData.py
- file_search.py
- file_write.py
- writeSumReport.py
avgData.py 1/4
[top][prev][next]
# Computes the average high temperature from a file that contains the daily
# high temperatures for last year at one location.
# CSCI 111
DATAFILE="data/virginia.dat"
tempFile = open(DATAFILE, "r")
sumOfTemperatures = 0
numberOfLines = 0
for line in tempFile:
#print(line)
numberOfLines += 1
# convert the line string into a float
temp = float(line)
sumOfTemperatures += temp
tempFile.close()
averageTemp = sumOfTemperatures/numberOfLines
print("The average daily high temperature is %.2f" % averageTemp)
file_search.py 2/4
[top][prev][next]
# Given a file and a term to search for,
# find which lines the term is on and the
# total number of lines that contained that term
# CSCI 111
FILENAME="data/years.dat"
print("This program will find out how many lines the given search term")
print("is on and show those lines")
print()
print("Looking at file", FILENAME)
searchFor = input("What term do you want to search for? ")
myFile = open(FILENAME, "r")
count = 0
places = []
lineTotal = 0
for line in myFile:
lineTotal += 1
# if the term that we are looking for is in the line
if searchFor in line:
print( searchFor, "is on line %d:" % lineTotal, line)
places.append(line)
count += 1
myFile.close()
print("The number of times \"" + searchFor + "\" was in a line is", count)
print(places)
file_write.py 3/4
[top][prev][next]
# Writes content from a user to a file
# by Sara Sprenkle
PROMPT = "Enter the next line in the file: "
outfilename = input("What is the name of your output file? ")
numLines = eval(input("How many lines do you want to write? "))
# create a new file object, in "write" mode
dataFile = open(outfilename, "w")
for x in range(numLines):
userinput = input(PROMPT)
# write the user's input to the file
dataFile.write(userinput)
# write a newline after each input from the user
dataFile.write("\n")
# close the file with the method "close"
dataFile.close()
writeSumReport.py 4/4
[top][prev][next]
# Given a file containing students names and their years (freshman, sophomore,
# junior, or senior) for this class, creates a report (in a file) that
# says the year and how many students from that year are in this class, on the
# same line.
#
# CSCI 111
FILENAME="data/years.dat"
REPORTNAME="data/report.dat"
print("This program will find out how many lines the given search term")
print("is on and show those lines")
print()
print("Looking at file", FILENAME)
print("Writing report to", REPORTNAME)
searchTerms = ["13", "14", "15", "16" ]
reportFile = open(REPORTNAME, "w")
# search for each of these terms
for searchFor in searchTerms:
myFile = open(FILENAME, "r")
# accumulate the number of times searchFor is in a line
# of the file
count = 0
for line in myFile:
# if the term that we are looking for is in the line
if searchFor in line:
count += 1
myFile.close()
# construct the output in the form:
# graduating_year number_of_students_in_that_year
outputLine = "%s %d\n" % (searchFor, count)
reportFile.write(outputLine)
reportFile.close()
Generated by GNU enscript 1.6.4.