Contents
- avgData.py
- file_search2.py
- file_search.py
- file_write.py
- writeSumReport.py
avgData.py 1/5
[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
total = 0
count = 0
dataFile = file("data/virginia.dat", "r")
for line in dataFile:
# line is a string, but we want it to be a float
temp = float(line)
# add the temperature to the accumulator
total += temp
# update my counter for how much data we read in
count+=1
# divide by how much data we read in
avgTemp = total/count
print "The average temperature is %.2f" % avgTemp
dataFile.close()
file_search2.py 2/5
[top][prev][next]
# This program allows you to search a file for a term and
# displays which lines of the file contain that term and
# a count of the number of lines that contained that term.
# CSCI 111
FILENAME="data/years.dat"
searchTerm = raw_input("What term are you searching for? ")
# creates a new file object, opening the file in "read" mode
dataFile = file(FILENAME, "r")
numOccurences = 0
lineNumber = 0
# reads in the file line-by-line and prints the content of the file
for line in dataFile:
#print line,
lineNumber += 1
if not line.startswith("#"):
if searchTerm in line:
print str(lineNumber) + ":", line,
numOccurences += 1
# close the file with the method "close"
dataFile.close()
print "We found", searchTerm, numOccurences, "times in the file"
file_search.py 3/5
[top][prev][next]
# This program allows you to search a file for a term and
# displays which lines of the file contain that term and
# a count of the number of lines that contained that term.
# CSCI 111
FILENAME="data/years.dat"
searchTerm = "SO"
# creates a new file object, opening the file in "read" mode
dataFile = file(FILENAME, "r")
numOccurences = 0
lineNumber = 0
# reads in the file line-by-line and prints the content of the file
for line in dataFile:
#print line,
lineNumber += 1
if searchTerm in line:
print str(lineNumber) + ":", line,
numOccurences += 1
# close the file with the method "close"
dataFile.close()
print "We found", searchTerm, numOccurences, "times in the file"
file_write.py 4/5
[top][prev][next]
# Writes content from a user to a file
# by Sara Sprenkle
PROMPT = "What do you want to add to the file? (Nothing will exit): "
outfilename = raw_input("What is the name of your output file? ")
# create a new file object, in "write" mode
dataFile = file(outfilename, "w")
while True:
userinput = raw_input(PROMPT)
if userinput == "" :
break
# 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 5/5
[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.
#
# Note: this is not the most efficient solution because we are going through a
# file multiple times, instead of checking for all the search times in the line
# at the same time. There is sometimes a tradeoff between ease of writing
# (e.g., using a function) and efficiency. Usually, the programmer's time is
# more valuable than computational time, but there are limits.
#
# CSCI 111
def main():
FILENAME = "data/years2.dat"
OUTFILE = "summary.txt"
sumFile = file(OUTFILE, "w")
numFY = getNumOccurences( FILENAME, "FY")
sumFile.write("FY \t" + str(numFY) + "\n" )
numSophomores = getNumOccurences( FILENAME, "SO")
sumFile.write("SO \t" + str(numSophomores) + "\n" )
numJuniors = getNumOccurences( FILENAME, "JR")
sumFile.write("JR \t" + str(numJuniors) + "\n" )
numSeniors = getNumOccurences( FILENAME, "SR")
sumFile.write("SR \t" + str(numSeniors) + "\n" )
sumFile.close()
# Given the name of a file (a string) and a search term (a string),
# returns the number of times that searchTerm occurs in the file
def getNumOccurences( filename, searchTerm):
# creates a new file object, opening the file in "read" mode
dataFile = file(filename, "r")
numOccurences = 0
# reads in the file line-by-line and prints the content of the file
for line in dataFile:
if not line.startswith("#") :
if searchTerm in line:
numOccurences += 1
# close the file with the method "close"
dataFile.close()
#print "We found", searchTerm, numOccurences, "times in the file"
return numOccurences
main()
Generated by GNU enscript 1.6.4.