Convert CSV to a HTML table format and store in a HTML file

后端 未结 3 1483
难免孤独
难免孤独 2020-12-22 06:08

I have tried few shell scripts to convert to a HTML table format but I didnt get desired output. Could anyone help here to convert CSV to a HTML table format using Python or

3条回答
  •  萌比男神i
    2020-12-22 06:54

    This may help you:

     import sys
     import csv
     if len(sys.argv) < 3:
        print "Usage: csvToTable.py csv_file html_file"
        exit(1)
    
     # Open the CSV file for reading
    
        reader = csv.reader(open(sys.argv[1]))
    
     # Create the HTML file for output
    
        htmlfile = open(sys.argv[2],"w")
    
     # initialize rownum variable
        rownum = 0
    
     # write  tag
    
       htmlfile.write('
    ') # generate table contents for row in reader: # Read a single row from the CSV file # write header row. assumes first row in csv contains header if rownum == 0: htmlfile.write('') # write tag for column in row: htmlfile.write('') htmlfile.write('') #write all other rows else: htmlfile.write('') for column in row: htmlfile.write('') htmlfile.write('') #increment row count rownum += 1 # write
    ' + column + '
    ' + column + '
    tag htmlfile.write('') # print results to shell print "Created " + str(rownum) + " row table." exit(0)

提交回复
热议问题