[Tutor] A Multiple Concatenation Problem
Mats Wichmann
mats at wichmann.us
Fri Sep 18 10:07:51 EDT 2020
On 9/18/20 7:34 AM, Stephen P. Molnar wrote:
>
>
> On 09/17/2020 02:00 PM, Alan Gauld via Tutor wrote:
>> On 17/09/2020 18:54, Mats Wichmann wrote:
>>
>>>>>> for suf in range(1, 11):
>>> ... filename = f"<ligand>{suf}.log"
>>> ... print(filename)
>>> ...
>>> <ligand>1.log
>>> <ligand>2.log
>>> <ligand>3.log
>>> <ligand>4.log
>>> <ligand>5.log
>>> <ligand>6.log
>>> <ligand>7.log
>>> <ligand>8.log
>>> <ligand>9.log
>>> <ligand>10.log
>>
>> In case that's not clear, Mats is using the new
>> format-string notation.
>> You can use the old style format strings similarly:
>>
>> fmt = "<ligand>.%d.log"
>> for suf in range(1,11):
>> print(fmt % suf)
>>
>> Or append to a list or...
>> rather than print...
>>
> I read the data in with:
>
> filename = 'Ligand.list'
> file = open(filename,mode='r')
> text = file.read()
> file.close()
>
> where Ligand.list, for testing, as at this point I have 31 ligands to
> process, contains
>
> 2-Pholoeckol
> 7-Pholoeckol
>
> If I use either:
>
> fmt = "<ligand>.%d.log"
> for suf in range(1,11):
> print(fmt % suf)
>
> or
> for suf in range(1, 11):
> filename = f"<ligand>{suf}.log"
> print(filename)
>
> I get the same result: 2-Pholoeckol.1.log to 2-Pholoeckol.10.log
> 7-Pholoeckol.1.log to 7-Pholoeckol.10.log
>
> Obviously I'm missing something very fundamental, and I'm quite
> embarrassed by that, but how di I get the ligand name rather than
> <ligand>... ?
You didn't say so, though I kind of suspected <ligand> wasn't literal,
but a placeholder. We just took it literally. You do the same kind of
string pasting again.
You have the names in 'text', so you want to loop over that. Imagining
something like this:
fmt = "%s.%d.log"
for ligands in text:
for suf in range(1, 11):
filename = fmt % (ligand, suf)
or using f-strings:
filename = f"{ligand}.{suf}"
More information about the Tutor
mailing list