[Tutor] A Multiple Concatenation Problem
Mark Lawrence
breamoreboy at gmail.com
Fri Sep 18 09:57:23 EDT 2020
On 18/09/2020 14:34, 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>... ?
>
Why not read the data from the file in a loop and process it? Something
like (completely untested):-
with open('Ligand.list') as infile:
for lineno, line in enumerate(infile, start=1):
filename = f"<line>{lineno}.log"
Or am I completely misreading your needs?
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
More information about the Tutor
mailing list