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()
serk
  • 4,329
  • 2
  • 25
  • 38
ArtisanSamosa
  • 847
  • 2
  • 10
  • 23
  • This is a homework question. See [this link for help on how to do this](http://stackoverflow.com/questions/7857337/how-to-store-rows-from-csv-file-into-python-and-print-data-with-html/7857554#7857554) and here is [the original CSV](http://www.cse.msu.edu/~cse231/Projects/proj06.dir/southPark.csv) you are trying to use. – serk Oct 24 '11 at 03: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. – ArtisanSamosa Oct 24 '11 at 03:29

1 Answers1

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])
bombnomnom
  • 61
  • 3
  • 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? – ArtisanSamosa Oct 24 '11 at 02:51
  • I forgot to set html equal to the return of the function. Edited above. – bombnomnom Oct 24 '11 at 03:07
  • 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? – ArtisanSamosa Oct 24 '11 at 03:54
  • 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. – bombnomnom Oct 24 '11 at 04:08
  • Also, I just found this where http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference they do a pretty good job of explaining Python's parameter passing method. – bombnomnom Oct 24 '11 at 04:10
  • Thanks a bunch for the help and guidance. I didn't reassign the output of the function and did that for each line. Then I just wrote each new html string to a file. – ArtisanSamosa Oct 24 '11 at 05:40