Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Replace Strings and Create HTML Files with Functions
    text
    copied!<p>I have a CSV file that I have to import and take each row of the file and print out in a HTML to create 5 separate files for each 5 rows in the CSV file. </p> <p><a href="http://www.cse.msu.edu/~cse231/Projects/proj06.dir/southPark.csv" rel="nofollow">Here</a> is a link to DL the CSV file. </p> <p>Here is my code so far:</p> <pre><code>import csv original = file('southpark.csv', 'rU') reader = csv.reader(original) BigList = [] html_str = """&lt;html&gt; &lt;P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in"&gt; &lt;FONT SIZE=7 STYLE="font-size: 60pt"&gt;VALUE1&lt;/FONT&gt;&lt;/P&gt; &lt;P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in"&gt; &lt;FONT SIZE=7 STYLE="font-size: 36pt"&gt;VALUE2&lt;/FONT&gt;&lt;/P&gt; &lt;P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in"&gt; &lt;FONT SIZE=7 STYLE="font-size: 36pt"&gt; VALUE3&lt;/FONT&gt;&lt;/P&gt; &lt;P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in"&gt; &lt;FONT SIZE=6 STYLE="font-size: 28pt"&gt; VALUE4&lt;/FONT&gt;&lt;/P&gt; &lt;P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in"&gt; &lt;FONT SIZE=6 STYLE="font-size: 28pt"&gt; VALUE5&lt;/FONT&gt;&lt;/P&gt; &lt;P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in"&gt; &lt;FONT SIZE=6 STYLE="font-size: 28pt"&gt; VALUE6&lt;/FONT&gt;&lt;/P&gt; &lt;P CLASS="western" ALIGN=CENTER STYLE="margin-top: 0.08in; margin-bottom: 0.25in"&gt; &lt;FONT SIZE=6 STYLE="font-size: 28pt"&gt; VALUE7&lt;/FONT&gt;&lt;/P&gt; &lt;/html&gt;""" for row in reader: BigList.append(row) print BigList html_str = html_str.replace('VALUE1', BigList[0][0]) html_str = html_str.replace('VALUE2', BigList[0][1]) html_str = html_str.replace('VALUE3', BigList[0][2]) html_str = html_str.replace('VALUE4', BigList[0][3]) html_str = html_str.replace('VALUE5', BigList[0][4]) html_str = html_str.replace('VALUE6', BigList[0][5]) html_str = html_str.replace('VALUE7', BigList[0][6]) html_str2 = html_str.replace('VALUE1', BigList[1][0]) html_str2 = html_str.replace('VALUE2', BigList[1][1]) html_str2 = html_str.replace('VALUE3', BigList[1][2]) html_str2 = html_str.replace('VALUE4', BigList[1][3]) html_str2 = html_str.replace('VALUE5', BigList[1][4]) html_str2 = html_str.replace('VALUE6', BigList[1][5]) html_str2 = html_str.replace('VALUE7', BigList[1][6]) def strToFile(): fout = open("marsh.html", "w") fout.write(html_str) fout.close() def strToFile2(): fout = open("broflovski.html", "w") fout.write(html_str2) fout.close() strToFile() strToFile2() </code></pre> <p>The result is that a "marsh.html" file is created which displays out the first row of the CSV file. What I can't figure out is how to loop it into def functions so I can replace each "VALUE1-7" because right now the first row only is stored. </p> <p>Obviously html_str2 and strToFile2() are pretty much useless since the first thing that is assigned to "VALUE1-7" is kept forever..</p> <p>SO the result I want is 5 files named marsh.html, broflofsvki.html, cartman.html, mccormick.html, and stotch.html with all their corresponding info from their respective rows displayed using the HTML template in the code above.</p> <p>I'm still confused on how to use lists and functions to be honest so any help or guidance is highly appreciated. If someone could show me how to do it I feel like I would understand more as I have searched through Google and tried to implement different methods myself and can't figure it out. I am using Python 2.7.2 btw.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload