Thursday, 19 October 2017

A script to convert CSV to HTML tables

HTML can be a pain to write manually, especially when it comes to tables.
This script does the basics. It asks for and tries to open up the source file, then you give it the name of the new file the HTML table will be saved to.
It works on the very simple idea of splitting each row by its separator (which the user needs to specify, such as ; or ,) and then inserting the HTML 'separators' of <td> and </td>. It could be improved on - at the moment HTML/CSS formatting and style needs to be entered manually after conversion and there is no allowance for the first row being different. Nonetheless, I believe it could save me a lot of time and bother.
#!/usr/bin/python3
import sys

print ("HTML Table Maker!")
filechoice = input("Please enter name of file with data, including file extension: ")
try:
    FX= open(filechoice, 'r')
except:
    print("Invalid File Name!")
    exit()

print(FX.readline())
FX.seek(0) # Returns reading point back to top of file
sep = input("Please enter column separator: ")

newfile = input("New File name (inc. extension)? ")
FN = open(newfile, 'w')
FN.write('<table>\n')
for line in FX:
    linelist = line.split(sep)
    FN.write('\t<tr>')
    for col in linelist:
        FN.write('<td>'+col+'</td>')
    FN.write('</tr>\n')
FN.write('</table>\n')    
FX.close()
FN.close()

The line FX.seek(0) is a new thing - I may have mentioned before that when a Python program is writing to or reading from a file, it maintains a progress point, like a theoretical cursor. 
fileobject.seek(linenumber) resets this progress point to a particular line (in this case back to the start). 
And the output is as follows:
>>> ================================ RESTART ================================
>>> 
HTML Table Maker!
Please enter name of file with data, including file extension: groceries.csv
Butter (250g), 1.50, 2, 

Please enter column separator: ,
New File name (inc. extension)? groceries.html
>>> 
And now I can embed the contents of groceries.html into this blog page which is based on HTML:

Item NamePrice per ItemNumber of Items
Butter (250g)1.502
Chocolate Biscuits (300g)1.502
Flour (1kg)0.503
Milk (1 pint)0.705
Pork Sausages (500g)4.501
Rice (1kg)1.701
Strawberry Jam (400g)2.001

or even add a bit of formatting:

Item NamePrice per ItemNumber of Items
Butter (250g)1.502
Chocolate Biscuits (300g)1.502
Flour (1kg)0.503
Milk (1 pint)0.705
Pork Sausages (500g)4.501
Rice (1kg)1.701
Strawberry Jam (400g)2.001

No comments:

Post a Comment