Homework 5 Questions- Spring 2008
Questions?
Please read all other questions and responses before you post your own.
The instructions say to name your function info() but where it says what the TA will type in to grade is media().
Which one do we use?
| info. The instructions have been changed. Thanks for pointing that out. Brittany Duncan |
Do you want us to post our pictures along with our code on t-square?
| No. Your code should work for any pictures and associated text files that fit the layout the assignment describes (text files with 6 lines, yadda yadda). Meaning we can supply our own, and your code should work with it. Chris Phillips |
how many text files should we use?
| Your code should work no matter how many text files are in the directory, so long as they're all formatted the right way (six lines), and have an associated jpg. Chris Phillips |
Maybe I'm just thick, but I still don't understand how we take the first three lines out of a .txt file (lastname, firstname, profession) without taking out the last lines (the trivia), and then put those three lines into the HTML code in JES.
There's a couple ways of doing it, but, the easiest would be using
list = file.readlines()
This will take every line from the file (meaning the file is now empty in JES, and should be closed), and place it into the list in a corresponding index; the first line from the file goes into index 0, the second line from the file goes into index 1, etc. I'll be going over this during my section's recitation, and you'll likely be shown it (or a variation of it) in yours. Chris Phillips |
| We will be covering reading and using slices (a.k.a. sub-strings) this week Colin Potts |
can we use random.sample also?
| Sure. If as long as it meets the requirements, you can implement your program however you like. Colin Potts |
I'm trying to use the readlines code mentioned above in order to read the lines and then put them one by one into my table and I have a little something like this:
textinfo1 = text1.readlines
file.write("<body><table><tr><td>" + textinfo1[1] + "," + textinfo1[0] + "(" + textinfo1[2] + ")" + "</td>")
and JES gives me an error stating that I am trying to access part of an object that doesn't exist. What's happening?
import os
def makePage(path):
filename = path + os.sep + "index1.html"
file = open(filename, "wt")
file.write("""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>The Simplest Possible Web Page</title>
</head>
<body>
<h1>A Simple Heading</h1>
<p>Some simple text.</p>
</body>
</html>""")
file.close() # close(file)
Does this make a new file called index1.html, or do you need to make a blank text file for it to open first?
for f in files and for num in range(1,4):
Is this a valid line of code? This would be used to go through a range of numbers to append to 3 different variables that would be returned and used in other places in the code.
No. Are you trying to do this for every f in files? If so, try "for f in files:
for num in range(1,4): " Thanks! Brittany Duncan |
And if not, if you're trying to, say, go to the first item in files, and have the number 1, and then go to the second item in files, and have the number 2, etc, you can use -
for f in range(1,4):
currentFile = files[f-1]
currentNumber = f Chris Phillips |
I have this code:
textfile1 = dir + os.sep + "infotables1.txt"
text1 = open(textfile1, "wt")
textinfo1 = text1.readlines()
file.write("<body><table><tr><td>" + textinfo1[1] + "," + textinfo1[0] + "(" + textinfo1[2] + ")" + "</td>")
text1.close()
And JES gives me an error saying:
The error was: 1
Sequence index out of range.
The index you're using goes beyond the size of that data (too low or high). For instance, maybe you tried to access OurArray[10] and OurArray only has 5 elements in it.
Please check line 15 of C:\Documents and Settings\Ryan\My Documents\hw5.py
I don't know how there can be too little data to draw from because there are 6 lines in the text file, so readlines should return 0 through 5 right?
| You're opening your file in write mode ("wt") instead of read mode ("rt"). This zeros out the file ready for writing and so overwrites anything that was originally in it. The error message that you're getting sounds misleading, but it's telling you that textinfo1 is an empty list. You will probably need to download the textfile again. If you check it now, I think you will find that it is now empty. Your indentation also looks wrong (i.e. first line vs. rest), but I think this is just how you have posted the code. Colin Potts |
JES produces source code that looks like this:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transition//EN" "http://wwww.w3.org/TR/html4/loose.dtd"><html><head><title>Ocean's 11</title></head><body><table border='3'><tr><td>Ocean
,Danny
(Mastermind
)</td><td><img src="C:\Documents and Settings\Ryan\My Documents\infotables1.jpg" /></td><td>Married to Tess Ocean
</td></tr><body><table border='3'><tr><td>Ryan
,Rusty
(Right Hand Man
)</td><td><img src="C:\Documents and Settings\Ryan\My Documents\infotables2.jpg" /></td><td>Wishes to invest in hotels
</td></tr><body><table border='3'><tr><td>Caldwell
,Linus
(Rookie
)</td><td><img src="C:\Documents and Settings\Ryan\My Documents\infotables3.jpg" /></td><td>Has good slight of hand
</td></tr></table></body></html>
and it looks fine except for the fact that my images won't show up. I don't know what to do at this point...
| Did you make sure infotables1.jpg, infotables2.jpg, and infotables3.jpg are in the folder My Documents? It may be that those pictures don't exist in that location so they can't be loaded Seung Gu |
| Take the body tag out of your loop! The final HTML should only have 1 <body> |
If our directory specifies our own unique file (such as dir + os.sep + "blah.jpg") how will it work for the TAs?
| It won't. You should not be using literal file names at all, otherwise there would be no point in writing a program; you could simply write the web page, and asking you to write a Python program to write the HTML would just be a pointless homework exercise. The whole point is to make this general. You should be reading the files from your directory into variables, such as dir + os.sep + jpgFile , where jpgFile is is the same as the text file you are currently reading but ends ".jpg" Colin Potts |
When typing
def title (titlestring):
return "<head><title><h2>" + titlestring + "</title></head></h2>"
i keep getting the error
>>> There was a spacing error in the program.
It might be from a previous line, but I think the error is in line 11
Any idea what may be wrong?
| Now that I have put code tags around your posting for you..... No, not really. Your Python looks fine. It could be that you are using smart quotes that don't show up on the posting on this page but that are confusing JES. Try erasing all the quotes and retyping them. Or it could be that there really is a problem before the def that is throwing things off – maybe faulty indentation above that point or a quote that hasn't been closed. Colin Potts |
| One other minor thing. The HTML you're generating isn't strictly correct, although it could work in some browsers. You should end tags in the reverse order to the order you start them. So you should close them in the order h2, title, head. Colin Potts |
How do you remove an item from a range? This will be used to take out one of the random lines from a range so the same line isn't used twice.
| You mean from a list? Use either del or list.remove(). See the Python library reference page http://docs.python.org/lib/typesseq-mutable.html for very brief documentation. Alternatively, if experimenting with that doesn't give you joy, you could easily write your own delete function that searches for the to-be-deleted item using find() and then uses the '+' operator to splice together a new list consisting of the sub-list before the target item and the sub-list following it. Colin Potts |
| You can also approach it differently, too. You know the items from the original list that you want (the last three), if you put those in a new list, and randomize them there (check the python API on the random module; google "python random"), you can then just iterate through the list using a for loop. Chris Phillips |
My pictures are showing in Internet Explorer, but not in Firefox. Could something in the code be causing this?
| Yes. Firefox is a bit stricter in general about HTML being valid. Are you putting the image file name in quotes? (BTW. It's excellent that you tested your page this way.) Colin Potts |
My program does all the requirements but for only one of the text files in my directory. How come its not creating a row for each of my text files and their picture instead of just one of them? Any possible suggestion as to what i need to change so my function will run for all the text files in the directory? Thanks.
| Do you have a for loop that looks for all the .txt files? Thanks! Brittany Duncan |
How do you read items in from a directory so you can assign variables to text files?
How do read the titles of the text files?
*snip*
I dont understand what i need to change to get my code to write out a new row for each text file its only doing it for one of them. Help please!
| Please do not post all of your code to the Coweb the night a homework is due. TAs are on AIM for a reason. Also, the reason that is happening is that you are creating a new hw5.html every time you iterate through your for loop. Thanks! Brittany Duncan |
Link to this Page