나는 $ i [ "CA :"+ $ i + ':'+ ""]의 루프 요소를 각각 대체하는 string Template을 사용하여 example.py 템플릿을 만들려고했습니다. 부분적으로는 작동하지만 마지막 요소 만 대체합니다.문자열 템플릿을 사용하는 동안 모든 루프 요소를 한 줄에 추가하는 방법은 무엇입니까?
그러나 모든 값을 특정 형식으로 한 줄에 추가하고 싶습니다. 예를 들어
:
일을 내 현재 스크립트는 것은 다음과
for i in range(1,4):
#It takes each "i" elements and substituting only the last element
str='''s=selection(self.atoms["CA:"+$i+':'+" "].select_sphere(10))
내가 얻고 것은 다음과 같다 :
s=selection(self.atoms["CA:"+3+':'+" "].select_sphere(10))
무엇, 나는 기대하고있다.
s=selection (self.atoms["CA:"+1+':'+" "].select_sphere(10),self.atoms["CA:"+2+':'+" "].select_sphere(10),self.atoms["CA:"+3+':'+" "].select_sphere(10))
내 스크립트 :
import os from string import Template for i in range(1,4): str=''' s=selection(self.atoms["CA:"+$i+':'+" "].select_sphere(10)) ''' str=Template(str) file = open(os.getcwd() + '/' + 'example.py', 'w') file.write(str.substitute(i=i)) file.close()
은 내가 원하는 출력을 얻기 위해이 두 개의 스크립트를 사용이 :
import os
from string import Template
a=[]
for i in range(1,4):
a.append(''.join("self.atoms["+ "'CA:' "+str(i)+""':'+" "+"]"+".select_sphere(10)"))
str='''s=selection($a).by_residue()'''
str=Template(str)
file = open(os.getcwd() + '/' + 'example.py', 'w')
file.write(str.substitute(a=a))
with open('example.py', 'w') as outfile:
selection_template = '''self.atoms["CA:"+{}+':'+" "].select_sphere(10)'''
selections = [selection_template.format(i) for i in range(1, 4)]
outfile.write('s = selection({})\n'.format(', '.join(selections)))
감사합니다. mhawke – DarwinCode