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
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('' + column + ' ')
htmlfile.write(' ')
#write all other rows
else:
htmlfile.write('')
for column in row:
htmlfile.write('' + column + ' ')
htmlfile.write(' ')
#increment row count
rownum += 1
# write
tag
htmlfile.write('
')
# print results to shell
print "Created " + str(rownum) + " row table."
exit(0)