Created
May 4, 2012 15:26
-
-
Save sammyrulez/2595517 to your computer and use it in GitHub Desktop.
print merge csv and jinja template
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
# -*- coding: utf-8 -*- | |
import csv | |
from jinja2 import Environment, FileSystemLoader | |
def read_input(csvfile , ): | |
csvfile_v = open(csvfile, "rb") | |
dialect = csv.Sniffer().sniff(csvfile_v.read(1024)) | |
csvfile_v.seek(0) | |
reader = csv.DictReader(csvfile_v, dialect=dialect) | |
return reader | |
def load_template(template_name, base_bath= '.'): | |
loader = FileSystemLoader(base_bath) | |
env = Environment(loader=loader) | |
template = env.get_template(template_name) | |
return template | |
def render_output(data,template,id_field): | |
base_name = template.name[0:template.name.index('.html')] | |
for d in data: | |
out = template.render(d) | |
f_out = open("%s_%s.html" % (base_name,d[id_field]) , "w") | |
f_out.write(out) | |
f_out.close() | |
def test_read_input(): | |
out = read_input('dummy.csv') | |
assert out != None | |
for r in out: | |
print r | |
def test_render_output(): | |
out = read_input('dummy.csv') | |
template = load_template('Testtemplate.html') | |
render_output(out,template,'SN') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment