Contents
- avgData.py
- file_write.py
- writeSumReport2.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/alaska.dat"
# open the file
dataFile = open(DATAFILE, "r")
# initialize the sum accumulator
sumOfTemperatures = 0
# initialize the number of lines in the file accumulator
numOfTemperatures = 0
# read each line of the file
for line in dataFile:
# convert the line to a float to represent the temperature
temperature = float(line)
# add the temperature to the sum
sumOfTemperatures += temperature
# update the number of temperatures
numOfTemperatures += 1
# close the file
dataFile.close()
# divide the sum by the number of temperatures in the file.
avgTemperature = sumOfTemperatures/numOfTemperatures
# display the results (twice) to show two different ways to use format.
print("The average temperature is {:.2f}".format(avgTemperature))
print("The average temperature is", "{:.2f}".format(avgTemperature))
file_write.py 2/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()
writeSumReport2.py 3/4
[top][prev][next]
# Given: a file containing students names and their years (first years,
# sophomore, junior, or senior) for this class
# Problem: create a report (in a file) that says the year
# and how many students from that year are in this class, on the same line.
# TODO: Write comments for the functions, improve user interface
# CSCI111
FILENAME="data/years.dat"
def main():
outFile = open("data/report.dat", "w")
lineList = getFileLines(FILENAME)
for year in ["First-Year","Sophomore", "Junior", "Senior"]:
numOccurences = countNumOccurrences(year, lineList)
writeData(outFile, year, numOccurences)
outFile.close()
def getFileLines(filename):
"""
"""
myFile = open(filename, "r")
lines = myFile.readlines()
myFile.close()
return lines
def countNumOccurrences(thing, strList):
"""
"""
count = 0
for string in strList:
if thing in string:
count += 1
return count
def writeData(outFile, label, count):
"""
"""
outFile.write( label + " " + str(count) + "\n")
# alternatively
#outFile.write("{:s} {:d}\n".format(label, count))
main()
writeSumReport.py 4/4
[top][prev][next]
# Given: a file containing students names and their years (first years,
# sophomore, junior, or senior) for this class
# Problem: create a report (in a file) that says the year
# and how many students from that year are in this class, on the same line.
# Shows different examples of how to write parameters.
# CSCI111
FILENAME="data/years.dat"
def main():
outFile = open("data/report.dat", "w")
for year in ["First-Year","Sophomore", "Junior", "Senior"]:
numOccurences = countNumOccurrences(year, FILENAME)
writeData(outFile, year, numOccurences)
outFile.close()
def countNumOccurrences(thing, filename):
"""
Takes as parameters two strings, thing and filename
Returns the number of lines where thing appears in the file
described by filename.
"""
count = 0
inFile = open(filename, "r")
for line in inFile:
if thing in line:
count += 1
inFile.close()
return count
def writeData(outFile, label, count):
"""
pre: outFile is a file object, label is a string, count is a number
Write a line of information to the file outFile, in the format:
label count
"""
outFile.write( label + " " + str(count) + "\n")
# alternatively
#outFile.write("{:s} {:d}\n".format(label, count))
main()
Generated by GNU Enscript 1.6.6.