Take the 2-minute tour ×

I'm trying to make some serial correspondence script in python. I have one file with template, and one with text table. How to use text table as substitution source?

Example imput: Template File:

Hello <name>,
We will be hosting some event in <town> bla bla...

Table with values

name      town       age   gender
Phillip   New York   22    male
Sonia     Warsaw     19    female

Expected output is 2 files with customised text.

share|improve this question
I can't figure out what you are trying to do - but whatever it is , it is wrong. You shuld not try to manipulate the result of pickled data - as you do on the line strZamien=strZamien.replace('$', '') - treat pickled data as a black-boxed byte stream that pickle can read back to you. –  jsbueno Feb 7 '13 at 20:41
Can you provide a short example of what output you want for some sample input? I can't quite figure out what you're trying to do. –  Mark Amery Feb 7 '13 at 20:50
I think answers for this question may be useful for someone, edited it to be more precise. Consider reopening it? –  sonia Feb 8 '13 at 2:31
add comment

closed as not a real question by wRAR, dawg, Andy Hayden, Troy Alford, Eric Feb 8 '13 at 0:55


2 Answers

up vote 1 down vote accepted

There are two parts to this. The first is parsing your text table to get a list of mappings of template placeholders to the values that need to be inserted. The second is the actual substitution of the values into the templates. Both are fairly easy.

Assuming that columns are separated by multiple spaces in your table, and that multiple spaces never form part of an actual column heading or value, you can use regular expressions to split each row into fields fairly easily and cleanly, and then the substitution of those values into the template is trivial.

import re

text_table = <something> # Replace with whatever you do to load the table
template_text = <something> # Replace with whatever you do to load the template

row_splitter = re.compile("  +") # Finds a sequence of two or more spaces
rows = text_table.split('\n') # Split the table into a list of rows
headings_row = rows[0]
headings = row_splitter.split(headings_row)

# Next we get a list of dictionaries mapping template placeholders to values
template_dicts = []
for row in rows:
    values = row_splitter.split(row)
    template_dict = dict(zip(headings, values))

# Next we substitute the values sets into the template one by one:
for template_dict in template_dicts:
    result_text = template_text
    for key, value in template_dict.iteritems():
        result_text = result_text.replace('<'+key+'>', value)
    print result_text # Or do whatever you like with it

Note that if you have control of the template file, you may want to replace your triangular-bracketed placeholders with braced placeholders (like 'Hello {name}, I see you are {age} years old'). Then you can just use String.format to do the substitution of values into the template for you, making the code even simpler.

share|improve this answer
this is what I was looking for, thanks :) –  sonia Feb 7 '13 at 23:49
add comment

import re

table_lines = open('your_table', 'r').read()
table = [ re.split(' +', l) for l in table_file[1:] ]

mail = open('your_template_dir', 'r').read()

for name,town,age,gender in table :
    re.sub('<name>', name, mail)
    re.sub('<town>', town, mail)
    re.sub('<age>', age, mail)
    re.sub('<gender>', gender, mail)

print mail

Personaly I advise you to use SQLite for your table.

share|improve this answer
it would be good if we knew all keywords, but my script should work for any input table and template, as long as table is providing enough information –  sonia Feb 7 '13 at 23:46
add comment
