0

Ok so basically I have a csv file with different values. I want each line from the csv needs to create a new html file. Each value in the line of the csv needs to replace the values1 - 7 in the html. I've tried to create a function to handle this, but I can't get it to change the values in the html. I can change the value manually, but I really want to know how to do it with a function. This would not only shorten the amount of coding, but make it more clean and efficient as well.

import string
import csv

#functions


#open the southpark csv file
def opensouthparkFile(openFile1):
    southparklist = []
    for i in openFile1:
        i.strip()
       l = i.split(",")
       southparklist.append(l)
    return southparklist



useinput = raw_input("Enter the name of the file that you would like to open:")
openFile1 = (open(useinput, "rU"))
openFile2 = open("Marsh", "w")
openFile3 = open("Broflovski", "w")
openFile4 = open("Cartman", "w")
openFile5 = open("McCormick", "w")
openFile6 = open("Scotch", "w")


southfile = opensouthparkFile(openFile1)



html = """
<html>
<P CLASS="western", ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=7 STYLE="font-size: 60pt">VALUE1</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=7 STYLE="fontSsize: 36pt">VALUE2</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=7 STYLE="font-size: 36pt"> VALUE3</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE4</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE5</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE6</FONT></P>
<P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in">
<FONT SIZE=6 STYLE="font-size: 28pt"> VALUE7</FONT></P>
</html>
"""



#Function for replacing html files with southpark values

def replacehtml(html, somelist):
    html = html.replace("VALUE1", somelist[0])
    html = html.replace("VALUE2", somelist[1])
    html = html.replace("VALUE3", somelist[2])
    print somelist[1]




replacehtml(html, southfile[0])
replacehtml(html, southfile[1])





openFile2.write(html)

openFile2.close()
2
  • This is a homework question. See this link for help on how to do this and here is the original CSV you are trying to use. Commented Oct 24, 2011 at 3:02
  • Yes it is, however your link does not help me answer my question. I need to know why my function is not editing the html string. Commented Oct 24, 2011 at 3:29

1 Answer 1

0

Python passes parameters by a scheme they refer to as "Call-By-Object." When you reassign the string in your replacehtml function, this doesn't change the original html string because strings are an immutable type.

Fastest fix is probably to change the string to a return of the function.

def replacehtml(html, somelist):
    html = html.replace("VALUE1", somelist[0])
    html = html.replace("VALUE2", somelist[1])
    html = html.replace("VALUE3", somelist[2])
    print somelist[1]
    return html

html = replacehtml(html, southfile[0])
Sign up to request clarification or add additional context in comments.

6 Comments

I tried this to no success. Should I write the function above the html string? Are there any other methods of doing what I am trying to accomplish?
I forgot to set html equal to the return of the function. Edited above.
THANKS! This is exactly what I needed. This was for line one I am still trying to figure out how I can do the same for the other lines in the list. Should I create a new html string or can this be done using the one I have?
If you don't reassign the output of the function to html, then html will be the same before and after the function. As such you could change "html = replacehtml" to "southhtml1 = replacehtml(html, southfile[0])" and "southhtml2 = replacehtml(html, southfile[1])" since html should remain the same between the function instances. Or you could define a list and append the return of replacehtml to it. There's a lot of ways to go about it, just remember that the first parameter to replacehtml will be the same before and after calling.
Also, I just found this where stackoverflow.com/questions/986006/… they do a pretty good job of explaining Python's parameter passing method.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.