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()
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.