Mikael Lövqvist

Text manipulation in python

by Mikael Lövqvist on

I am currently working on a project were I need to be able to handle text in a trivial way.
In order to do this I made a simple construct in python for this, you can have a look at the implementation here text_manipulation.py

Here follows an example of how this could be used:

variable T is first assigned indented_block to make things easier to type.
Then a program template is created where we have two markers, ¶includes and ¶main.
We also create a template for c functions named function.

In the example we replace ¶includes with a list of T("#include <file>") replacing file with the actual filenames. Then we define the body of the main function. We finally use the replace_kw function that will perform several replacements, one for each keyword argument.
Using the function template we can create out main function and also match indent level.

And here is the output of the program:

Mikael Lövqvist

Written by: Mikael Lövqvist