0

I was able to generate html file per row.

my current output for "text_file 1.html" is the header: ['ID', 'Name', 'Type', '3 Year Funds Offered', '3 Year Funds Sold', 'Est. Most Recent Fund Date', 'Investor Location', 'Investor City', 'Investor State', 'Investor Country', 'Portfolio Size', 'Number of Deals', 'Website', 'Average Growth Score']

But I want to customize the table/data and keep the Header for each data from csv.

Here's my sample code:

import csv

    with open('test.csv', 'r') as csvfile:
           r = csv.reader(csvfile, delimiter=',')
           cnt=0
           for row in r:
               cnt+=1
               file_path="text_file %s.html" % (str(cnt),)
               with open(file_path,"w") as text_file:               
                   text_file.write(str(row)+"\n")`enter code here`

Do you have any tips so I can run this correctly? Thanks!

4
  • Can you edit your question to show the expected output? Are you trying to write each row to a separate file, or maybe you want to create an HTML table in a single file? Commented Mar 22, 2017 at 17:16
  • I misread for my last comment. Keeping with your current code, define something like my_headers = ['ID', 'Name', 'Type', '3 Year Funds Offered', '3 Year Funds Sold',...] before with open('test.csv', 'r') as csvfile: and then under with open(file_path,"w") as text_file: have another line that writes your headers like text_file.write(','.join(item for item in my_headers)+"\n"). And then look at pandas to_html and use that instead :) Commented Mar 22, 2017 at 17:23
  • Actually my expected output is just a regular table with header and 1 row below. I have 2000 rows from my csv file. I was able to generate html each row already using my current py code. Commented Mar 22, 2017 at 17:31
  • Will you give me your sample code for this? Commented Mar 22, 2017 at 17:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.