Sublime Forum

Creating a text file based on inputs from template and data text files

#1

I have a text file as shown below - This is the input text file (can be CSV or tab de-limited):

File:1
Template	Item	Item_Colour	Item_wt	Grows_in Salad 	Raw
fruit		Apples 	Red_Green	300	snowy
fruit		Orange 	Oranges 	250	Sunny
fruit		Grape 	Violet		25	Sunny
Vegetable	Carrot	Red   		100 	Hilly 	Yes 	Yes

The first column template describes the text format (the template) that has to be generated. The subsequent columns the item that has to be populated in the selected template.

The output file should look something like this:

File:2
Template: Fruit
Selected fruit :Apple
Apple color is Red_Green
The average weight of Apple is 300 gram
Apple grow in regions that are snowy

Template: Fruit
Selected fruit :Orange
Orange color is Orange
The average weight of Orange is 250 gram
Orange grow in regions that are sunny

Template: Fruit
Selected fruit :Grape
Grape color is Violet
The average weight of Orange is 25 gram
Grape grow in regions that are sunny

Template: Vegetable
Selected Vegetable :Carrot
Carrot can be used in salad
It can be eaten raw
Average weight of carrot is 100 gram
Carrot grow in regions that are Hilly

The template_file can be in file:1 or can be separate file.

I’m using Sublime text 3 (free edition) as the text editor

I’ve learnt about Snippets and Emmet through YouTube.
I do not know how to process two files to generate a result

There are multiple instances of this and several other templates one below the other within the same text file.

0 Likes

#2

I’m working on a Macro Plugin for this sort of thing.  Still have a bit of work to do, so it may be a few weeks before it’s ready for release.
 



 
Another approach would be to use a custom plugin to parse your CSV file & format the data accordingly.

 
I wrote a simple proof of concept for your particular issue:

Code & More Info @ GitHub

 



 
Alternatively, you could refactor the new_file command inside of the loop, so that each of your templates is written to a new file.

You could also utilize the file_SaveAs command to automatically save each file with a naming convention of your choosing.

2 Likes

#3

Thanks for your excellent code!. This will be a good place to start for me see , how I could utlize your code to build on the original requirements.

1 Like

#4

 
In what ways do you want to build on the original requirements?

0 Likes

#5

The problem I’ve stated here is a simple version of the original problem. My actual case has lots of lines (around 50 - 100 lines) in each of the template and the number of variables to be updated are around 5-10. So I know how to build the template from your code.

Note: I was using snippet in ST, but that is a pain if you miss on one of the tabs (place holder).

Do you know any other way of implementing this easily?

0 Likes

#6

 
Not really, considering that your end result requires arbitrary text to wrap each value with.  That’s something you pretty much have to define explicitly.

I was going to suggest making a template in Excel with VBA, but remembered that you have some lines which require multiple values like:
"Average weight of " + item + " is " + item_wt + " grams".

It’s probably easier to manage that with SublimeText + Python than it would be with Excel + VBA.

1 Like

#7

@fico Yes I tried Excel VBA route too as you suggested. But the problem is that this file is an input file to a legacy system developed in 1968. It would throw an error if it will not understand a line return or any special character that Excel might insert. As you suggested ST + Python looks like the way to go!

1 Like

#8

Although this can be done with Sublime, it sounds like you’ve given a simplified example of the dataset and requirements you’re dealing with. There comes a point where using the right tool might pay off in the long run, and if your source data has come from a database then querying the database and building a report is definitely easier and more robust. It all depends on the circumstances surrounding what you’re doing.

For example, if you want to do that in a webby way, a PHP script would be a nice solution. The advantage being if fields or layout changes your script is far less likely to break, and also easily modify the report output formatting.

0 Likes