I am new to Python and I am writing my first utility as a way to learn about strings, files, etc. I am writing a simple utility using string replacement to batch output HTML files. The program takes as inputs a CSV file and an HTML template file and will output an HTML file for each data row in the CSV file.
CSV Input File: test1.csv
The CSV file, which has header row, contains some catalog data, one product per row, like below:
stockID,color,material,url 340,Blue and magenta,80% Wool / 20% Acrylic,http://placehold.it/400 275,Purple,100% Cotton,http://placehold.it/600 318,Blue,100% Polyester,http://placehold.it/400x600
HTML Template Input File: testTemplate.htm
The HTML template file is simply a copy of the desired output with string replace tags %s
placed at the appropriate locations:
<h1>Stock ID: %s</h1>
<ul>
<li>%s</li>
<li>%s</li>
</ul>
<img src='%s'>
The Python is pretty straight forward I think. I open the template file and store it as a string. I then open the CSV file using the csv.dictreader()
command. I then iterate through the rows of the CSV, build the file names and then write the output files using string replacement on the template string using the dictionary keys.
import csv
# Open template file and pass string to 'data'. Should be in HTML format except with string replace tags.
with open('testTemplate.htm', 'r') as myTemplate:
data = myTemplate.read()
# print template for visual cue.
print('Template passed:\n' + '-'*30 +'\n' + data)
print('-'*30)
# open CSV file that contains the data and store to a dictyionary 'inputFile'.
with open('test1.csv') as csvfile:
inputFile = csv.DictReader(csvfile)
x = 0 # counter to display file count
for row in inputFile:
# create filenames for the output HTML files
filename = 'listing'+row['stockID']+'.htm'
# print filenames for visual cue.
print(filename)
x = x + 1
# create output HTML file.
with open(filename, 'w') as outputFile:
# run string replace on the template file using items from the data dictionary
# HELP--> this is where I get nervous because chaos will reign if the tags get mixed up
# HELP--> is there a way to add identifiers to the tags? like %s1 =row['stockID'], %s2=row['color'] ... ???
outputFile.write(data %(row['stockID'], row['color'], row['material'], row['url']))
# print the number of files created as a cue program has finished.
print('-'*30 +'\n' + str(x) + ' files created.')
The program works as expected with the test files I have been using (which is why I am posting here and not on SO). My concern is that it seems pretty fragile. In 'production' the CSV file will contain many more columns (around 30-40) and the HTML will be much more complex, so the chances of one of the tags in the string replace getting mixed seems pretty high. is there a way to add identifiers to the tags? like %s1 =row['stockID'], %s2=row['color'] ...
? that could be placed either in the template file or in the write()
statement (or both)? Any method alternatives or improvements I could learn would be great (note I am well aware of the Makos and Mustaches of the world and plan to learn a couple of template packages soon.)