Created
November 5, 2012 12:15
-
-
Save enigmaticape/4016913 to your computer and use it in GitHub Desktop.
Very simple CSV to HTML table in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import os | |
import csv | |
import string | |
if len( sys.argv ) < 2 : | |
sys.stderr.write( sys.argv[ 0 ] + | |
": usage - " + | |
sys.argv[ 0 ] + " [.csv file name]\n" ) | |
sys.exit() | |
if not os.path.exists(sys.argv[ 1 ]): | |
sys.stderr.write( sys.argv[ 1 ] + " not found \n" ) | |
sys.exit() | |
with open( sys.argv[ 1 ], 'rb') as csvfile: | |
table_string = "" | |
reader = csv.reader( csvfile ) | |
for row in reader: | |
table_string += "<tr>" + \ | |
"<td>" + \ | |
string.join( row, "</td><td>" ) + \ | |
"</td>" + \ | |
"</tr>\n" | |
sys.stdout.write( table_string ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very simple CSV to HTML table code, blogged at http://www.enigmaticape.com/blog/simple-python-script-for-simple-csv-to-html-table-rows/