From string library, template function doesnt work with int value?
<p>I am trying to use Template from string. I have a directory with 100 csv files with data pertaining to each year.</p>

<p>For ex.:</p>

<pre><code>yob1881.txt
yob1882.txt
yob1883.txt
yob1884.txt
yob1885.txt
</code></pre>

<p>Now i want to use template so that i can loop over all files. So i am using a range function like:</p>

<pre><code>for year in range(1880,2011):
    template = Template(/name/year$year)
    template.substitute(year)
</code></pre>

<p>This is returning an error:</p>

<pre><code>TypeError                                 Traceback (most recent call last)
&lt;ipython-input-4-85f21050945a&gt; in &lt;module&gt;()
      2 filepath = tp('/pythonDataProjects/Loan Granting/names/yob$year.txt')
      3 year = '1880'
----&gt; 4 print(filepath.substitute(year))

/Users/omkar/anaconda/lib/python3.5/string.py in substitute(*args, **kws)
    127             raise ValueError('Unrecognized named group in pattern',
    128                              self.pattern)
--&gt; 129         return self.pattern.sub(convert, self.template)
    130 
    131     def safe_substitute(*args, **kws):

/Users/omkar/anaconda/lib/python3.5/string.py in convert(mo)
    117             named = mo.group('named') or mo.group('braced')
    118             if named is not None:
--&gt; 119                 val = mapping[named]
    120                 # We use this idiom instead of str() because the latter will
    121                 # fail if val is a Unicode containing non-ASCII characters.
</code></pre>

<p>TypeError: string indices must be integers</p>

<p>I know what the error is. However, i am not getting how to resolve it.</p>

<p>Any help? </p>

<p>You should pass the parameter as a named argument:</p>

<pre><code>for year in range(1880, 2011):
    template = Template('/name/year$year')
    t = template.substitute(year=year)
</code></pre>

python string python-3.x templates