[Tutor] Incorporate an Integer as part of a Label
Peter Otten
__peter__ at web.de
Sat Jan 14 04:53:43 EST 2017
S. P. Molnar wrote:
> I am a very new python pseudo-programmer (Fortran II was my first
> language eons ago) and have encountered a problem that is giving me fits
> (google is no help).
>
> I have written a python program that takes the output from a quantum
> chemistry program, carries out some calculations and saves two files a
> figure as a png file and the spectrum of the molecule as a csv file.
>
> The problem that I am having is incorporating the numerical designator
> of the molecule in the names of the files.
>
> I've managed to figure out how to input the numerical designator:
>
> name_1 = input("Enter Molecule Number: ")
> name_1 =str(name_1)
> print('Name 1 = ',name_1)
> name = name_1 + '.out.abs.dat'
> data_1 = np.loadtxt(name, skiprows=0)
>
> As there are two different output files involved with each orca
> calculation I go through the above lines again for the second file with
> the dame numerical integer input.
>
> The problem is how do I get teh numerical molecule designator into the
> label I want to use for the two results of the Python calculation?
>
> plt.title("Absorption Spectrum (<number>)")
>
> plt.savefig('<number>.png', bboxinches='tight')
>
> and
>
> with open('<number>.csv', 'w') as f:
>
> where <number> is the integer molefule number designator that is the
> only input for the calculation.
Have a look at the str.format() method:
>>> number = 42
>>> title = "absorption spectrum {}".format(number)
>>> title
'absorption spectrum 42'
There are many bells and whistles:
>>> filename = "data-{number:05}.{suffix}".format(number=number,
suffix="csv")
>>> filename
'data-00042.csv'
See <https://docs.python.org/dev/library/string.html#formatspec>
for the details.
More information about the Tutor
mailing list