Then this lab is due at the time your lab starts on Friday.
You may be excused from lab on Friday Nov 12 ONLY if you submit lab07, lab08 and lab09 before your lab time starts on Friday Nov 12th. In that case, you may turn in your H11 in lecture on Tuesday 11/16. (No 2nd turnins though.)
You get a zero on both lab09 and H11.
Finally, please understand: being excused from lab on 11/12 is a privilege I'm offering, not a right. As far as I'm concerned, 11/12 is a normal work day at the University, and you are expected to be in class, period—as I told you a couple of weeks ago. So by offering an opportunity around that, I'm just being nice.
So don't push it.
This lab builds on the concepts of opening files, reading from files and closing files that we introduced in lab07.
In this lab, we add the concept of output files.
By the time you have completed this lab, you should be able to understand the concepts of reading from one file, and writing into another.
In particular, you'll be able to:
The preliminaries for this lab are similar to those for previous labs.:
In lab07, we worked with reading data from a file called
Open up that file in IDLE (use the File/Open menu) and save a copy of that file in your cs8/lab09 directory.
Here is a reminder of the kind of data that should be in that file:
SHARON, ROBINSON, PHYSICS BRIAN, CLARK, MUSIC MICHELLE, RODRIGUEZ, UNDEC RONALD, LEWIS, MATH LAURA, LEE, ENGLISH ANTHONY, WALKER, UNDEC SARAH, HALL, CS KEVIN, ALLEN, CHEM KIMBERLY, YOUNG, UNDEC JACOB, HERNANDEZ, STATS
In lab07, we wrote some functions that would help us analyze this data.
In this lab, we are going to look at how we could put this data on a web page.
Each line in the input file has the following format:
FIRSTNAME, LASTNAME, MAJOR
On a web page, we want this data to be in a table like this one:
We will use Python's ability to write to files (explored on Homework H11) to accomplish this.
Back at Step 1, you were supposed to copy some code from makeWebPageExample.py into a file with the same name, and save it. Now, run that file, and at the Python prompt, type this:
It should look something like this when you do it—except that instead of pconrad, you'll see your own username:
>>> convertStudents2Table("students.txt","table.html") Look for the web page at this URL:
http://www.cs.ucsb.edu/~pconrad/cs8/lab09/table.html Or if working on a PC or Mac, at this one:
If you read the output carefully, you'll see that what happens next depends on whether you are on CSIL, or on your own PC/Mac—but either way, it involves copying a web address into a browser. When you open up that web address, you should see a table like the one shown above, but with your own data.
If it doesn't work, make sure you have a students.txt file in the directory where you are running IDLE. You may need to do the same troubleshooting steps listed under "what if it doesn't work" in the instructions for lab07.
Assuming it does work, though, go on to the next step.
Create a another file similar to
students.txt—in the same format, with first name, last name, and major separated by commas—but with different names and majors. Save it with another name, say
convertStudents2Table function again—this time, putting in the name of your data file, e.g. "friends.txt" as the first parameter, and "friends.html" as the second parameter.
You should see another web page—this time with the new data you put in your "friends.txt" file formatted as an HTML page.
Now, you need to look through the code in makeWebPageExample.py and try to understand how it works.
Note that you do NOT need to spend much time on the following functions—these are supplied for you to make your lives a little easier, but are not that important for you to focus on at this time:
We'll briefly introduce each of those a little later in the lab—but for the most part, you won't need to touch those.
Instead, skip on down to the definition of the function that we DO want to focus on—the one for which you typed in a function call at the Python prompt—the definition that starts like this:
This function takes two strings as parameters (e.g.
The first is used as the filename in a call to open that opens an input file:
studentFile = open(inputFileName,"r")
The second is used as the last part of a filename in a call to open that opens an output file.
The first two lines below create a directory for web pages under your home directory and build up a file name (using string concatenation) that starts with ~/public_html/cs8/lab09 and ends with the value of outputFileName.
They do so using some functions I'm supplying for you that take care of the complicated stuff (looking up your home directory and your username). (You don't need to worry about the internal details of those functions.)
makeWebDirectoryIfItDoesNotExistAlready() htmlFileName = webDirectory() + "/" + outputFileName htmlFile = open(htmlFileName,"w")
Now, we have both an input file and an output file to work with.
The first thing we do is write the "header" to the HTML output file.
This is done via these lines:
htmlFile.write("<html>\n") htmlFile.write(webPageHeader("Table of Students"))
We then have code which writes out the body of the page which contains an H1 heading, and table.
To make sense of the code, you'll need a bit of an overview of how HTML tables work:
<table>and ends with
<tr>and end with
<th>...</th>elements (table headers) or
<td>...</td>elements (table data).
<tr><th>First Name</th><th>Last Name</th><th>Major</th></tr>
For more information on HTML Tables, see: http://www.w3schools.com/html/html_tables.asp
So, we see in the code, some lines that write the first part of the body to the output file:
htmlFile.write("<html>\n") htmlFile.write(webPageHeader("Table of Students")) htmlFile.write("<body>\n") htmlFile.write("<h1>Table of Students</h1>\n") htmlFile.write("<table border='1'>\n") # Write the table header htmlFile.write("<tr><th>First Name</th><th>Last Name</th><th>Major</th></tr>\n")
Then, there is a loop that reads every line from the file:
# process every line in the file---and for each one # write a line to the html for the web page
for line in studentFile:
What do we do for each line? We split it into a list of items, and then print those items, formatted as a row in an HTML table, like this:
# Convert this line into a list lineAsAList = line.strip().split(",") # Pull out the first name, last name and major fname = lineAsAList.strip() lname = lineAsAList.strip() major = lineAsAList.strip() # Write one line to the table htmlFile.write("
" + "") " + fname + "" + " " + lname + "" + " " + major + "" + "
Only after the entire loop is finished, we write the end of the table, the end of the HTML file, and then close up the files:
htmlFile.write("</table>\n") htmlFile.write("</body>\n") htmlFile.write("</html>\n") # close the intput file and the output file studentFile.close() htmlFile.close()
There is then a little more code that prints out some messages for the user, telling them where to look for the web page. The output is something the user can copy/paste into the web address bar of their browser to find the file on the web.
Once you think you understand what is going on, you are ready for the challenge of this week's lab.
The file sb2009weather.csv is a file that contains actual weather data for Santa Barbara for every day during the 2009.
Look over this file. Here are the first few lines:
20090101,0.01,61,35,N,2,95.5,61.7 20090102,0,57,39,NE,2,93.5,74.5 20090103,0,59,42,NE,2,94.6,61.3 20090104,0,57,39,NE,3,77.8,38.1 20090105,0,60,37,NE,3,82.5,42.2 20090106,0,62,41,N,2,87.9,58.4 20090107,0,65,41,N,2,88.4,51.1
Each line contains these values, separated by commas:
Your job: write a program that reads all the data from this file, and formats it as a web page, so that it looks like this:
You can use the makeWebPageExample.py as a starting point—but be sure to make appropriate changes:
Your code should work by providing a function that you can call like this:
You may want to save a small version of your file—say just the first few lines—to a file called "test.csv" and use that when testing.
When your function can turn any weather file into one like the one shown above, then you are almost done with lab09!
To get full credit on this lab, both partners (or all three partners) need to be showing the web page under their account.
So, before submitting via "turnin", make sure that for both (or all three partners), you can see the web page at:
To do that, you need to:
makeWeatherWebPagein each person's account.
But before submitting, do your final inspection—look over the rubric, make sure everything looks ok.
Then submit using:
turnin lab09@cs8 lab09
This lab follows the practice introduced in lab07 where:
Lab09 grading rubric:
Copyright 2010, Phillip T. Conrad, CS Dept, UC Santa Barbara. Permission to copy for non-commercial, non-profit, educational purposes granted, provided appropriate credit is given; all other rights reserved.