For something I'm working on I need to be able to create a large number of files by filling in fields in a template file with entries from a data set. You'd think that would be easy with Linux but I couldn't find a way to do it. (This will be where people tell me a thousand different ways to do it) I didn't think what I wanted was complicated so I wrote SimpleMerge to take care of it. It is a basic Python script that takes data from a tab delimited data file and fills in data fields in a template file.
The first row of the data file are the field identifiers to find and replace and the other rows are just data. This file can be easily generated from a spreadsheet program. The template file contains the structure of the file you intend to create, just with field identifiers in the place of real data.
I haven't done extensive testing on the program but it seems to work fine. It handles UTF-8 file encoding and maintains the line endings of the template file for both UNIX and Windows systems. The following command generates the two files File1.txt and File2.txt as seen in the block diagram below.
SimpleMerge.py template.txt Data.txt
Simple Merge Block Diagram |
SVG Template |
to this in a matter of minutes. Just by replacing colour and three text fields.
Generated Images |
I make no guarantee as to how well this works. So my advice is to back things up before using it. Have fun.
Get The Code! |
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.