Python substitute template


















Learn more. Python: Using template and substitute in subroutine Ask Question. Asked 9 years, 11 months ago. Active 9 years, 11 months ago. Viewed times. Template ''. Any ideas what the problem could be? Improve this question. I don't understand. The variables name, or variables value?

Add a comment. Active Oldest Votes. Improve this answer. This is also a part of our earlier snippet, which performs a template substitution from mapping to the keyword arguments kwargs. Therefore, it is passed as a Dictionary for template substitution. This is similar to substitute , except that if placeholders are missing from mapping and kwargs , instead of raising a KeyError exception, the original placeholder will appear in the resulting string intact. As you can see, there is no KeyError , resulting in an incomplete, but error-free substitution.

The Template object has the template attribute, which returns the template string. I open the template file and store it as a string. I then open the CSV file using the csv. I then iterate through the rows of the CSV, build the file names and then write the output files using string replacement on the template string using the dictionary keys. The program works as expected with the test files I have been using which is why I am posting here and not on SO. My concern is that it seems pretty fragile.

In 'production' the CSV file will contain many more columns around and the HTML will be much more complex, so the chances of one of the tags in the string replace getting mixed seems pretty high. Any method alternatives or improvements I could learn would be great note I am well aware of the Makos and Mustaches of the world and plan to learn a couple of template packages soon.

Python has a number of templating options, but the simplest to start is probably the string. If you need more output options, look at the string. Python has a style guide called PEP8. Among many other great things, it gives guidelines about spacing that you do not follow.

Indeed, your spacing seems to be quite inconsistent. You can also use tools such as pylint to check your code. Among other things, Python naming convention are now followed. I can see 30 in multiples places. This is usually a bad sign : if you ever want to change the value to something else, you'll have to change it in multiple places. You probably should define a constant to hold that value behind a meaningful name. Fixing the style After reading through PEP8 and the supporting docs, you will gain a deeper insight into the importance of style in your Python code.

As Josay mentioned in his thoughtful answer, the style of the code is poor spacing issues, line lengths, naming conventions leading to poor readability. In addition to using the autopep8 tool mention in Josay's answer, some of the style issues can be fixed with changes to methods as explained below. I am using Sublime Text 3 as my editor, so there are also several features available to ensure proper styling on future code.

Some quick changes to the user settings in ST3 include can include:. In addition, a linting package for ST3 can be installed and used to highlight style issues as you code. I selected the SublimeLinter3 package with the pep8 plug-in.

A more robust string substitution method: As mentioned in the original post, the simple string replace method of using the string formatting operator like:. It works fine for very short strings like this example, but not for longer strings like documents. Mix up your list and your bar suddenly runs into a foo! DictReader into a dictionary with column heads as the keys, so a slightly better approach would be to at least use the optional mapping key on the string formatting operator to take advantage of that.



0コメント

  • 1000 / 1000