1

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.

serk
  • 4,329
  • 2
  • 25
  • 38
Goose
  • 2,130
  • 10
  • 32
  • 43
  • 5
    Ouch. Your HTML hurts my eyes. – gilly3 Oct 25 '11 at 00:47
  • You two must be in the same college class. Similar to this previous post: http://stackoverflow.com/questions/7870694/create-function-to-replace-index-in-string-with-index-in-list-then-save-those-v – serk Oct 25 '11 at 01:24

1 Answers1

0

Here are some hints for what appears a bit like homework.

You already know how to parse the csv file, so you've done quite the hardest. Now, you just have to use the content of each line correctly.

A function is useful when you need to do execute the same task several times, or to execute similar tasks. A list is useful to store data in sequence, and Python is really good at using such sequences (with for statement).

So. The files you want to create should be named by characters, and you have their names in the csv file; so you can use this data as name for your html files.

Then, all your html paragraphs are the same whatever the value inside it, so maybe you could write a function, that would take a value as argument, and would return the html string containing the value as result. The header and footer of the html file would, obviously, have to be be generated separately.

Please consider rewriting your code, and edit your question with another attempt. I'll be pleased to guide you in achieving this exercise.

By the way, gilly3 is right: please, please, remove styling from your html tags, and consider css for this.

Joël
  • 2,723
  • 18
  • 36