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.
Here is a link to DL the CSV file.
Here is my code so far:
import csv
original = file('southpark.csv', 'rU')
reader = csv.reader(original)
BigList = []
html_str = """<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="font-size: 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>"""
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()
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.
Obviously html_str2 and strToFile2() are pretty much useless since the first thing that is assigned to "VALUE1-7" is kept forever..
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.
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.