Formatting csv file data using html template

I have a csv file, data and an HTML file, a template .

I want the script to create a separate html file for each entry from the csv file, using the html file as a template.

What is the best way to do this in Ruby? Python Is there a tool / library that I can use for this in any language?

+4
source share
2 answers

Ruby built CSV processing, which should make it pretty simple to output static HTML files.
Cm:

Actually, so is Python, so this is really a matter of personal preference (or what you have already set up).

+4
source

Python with Jinja2 .

import jinja import csv env= jinja.Environment() env.loader= jinja.FileSystemLoader("some/directory") template= env.get_template( "name" ) rdr= csv.reader( open("some.csv", "r" ) ) csv_data = [ row for row in rdr ] print template.render( data=csv_data ) 

Turns out you could leave just by passing rdr directly to Jinja for parsing.

If the template looks like this, it will work with a lot of Python structures, including an iterator.

 <table> {% for row in data %} <tr> <td>{{ row.0 }}</td><td>{{ row.1 }}</td> </tr> {% endfor %} </table> 
+4
source

Source: https://habr.com/ru/post/1286464/


All Articles