문제점을 해결하기 위해 파일의 변수와 파일 이름을 바꾸는 것이 목표였습니다. 문제는 모든 조합 (일반적으로 24 개 조합)에 대해 두 개의 변수를 동시에 변경해야한다는 것입니다.파일에서 변수 쌍 바꾸기
모든 문자열 조합을 만드는 방법을 알고 있지만 목록을 안에 넣고 반복합니다. 나는 그 함수를 호출 할 때 변수의 이름을 전달할 수 있습니다
def replace_variables(distance ='0', T1 ='0', T2 = '0', gamma = '0'):
template_new = template.replace('*distance*', distance).replace('*T1*', T1).replace('*T2*', T2).replace('*gamma*', gamma)
input_file = input_name.replace('one','T1'+T1).replace('two','T2'+T2).replace('phi','PHI'+Phi).replace('distance','R'+distance)
return template_new, input_file
:
a = [ 'distance', 'T1', 'T2', 'gamma' ]
new_list = list(itertools.permutations(a, 2))
나는 내 값을 전달하는 기능을 만들었습니다.
distance = ['-3','+3']
T1 = ['-3', '+3']
T2 = ['-3', '+3']
gamma = ['-3', '+3']
그리고 변수의 각 쌍의 파일과 같은 파일의 이름 값을 변경할 경우 :
원본 파일 : name_file_R_T1_T2_gamma.txt
for i in new_list:
elem1 = i[0]
elem2 = i[1]
template_new, input_file =replace_variables(elem1, elem2)
print input_file
나는 목록을 사용할 필요가 있지만
은 다음으로 대체됩니다.
name_file_3_3_0_0.txt, name_file_3_-3_0_0.txt, name_file_-3_3_0_0.txt,
name_file_3_3_0_0.txt, name_file_3_0_3_0.txt, name_file_3_0_-3_0.txt,
등이있다.
원래 템플릿은 다음과 같습니다
template = """
R = 3.0 *distance* cm
THETA1 = 60. *T1* degree
THETA2 = 2.0 *T2* degree
GAMMA = 0 *gamma* degree
"""
내가 가져올 :
template = """
R = 3.0 +3 cm
THETA1 = 60. +3 degree
THETA2 = 2.0 +0 degree
GAMMA = 0 +0 degree
"""
와 나는 거의 위의 문제 달려 드는 생각 등
귀하의 게시물이 불분명합니다. 템플릿에 대한 추가 정보를 제공 한 경우 일 수 있습니다. – Kosch
그래서'input_name = "name_file_R_T1_T2_gamma.txt"'? 당신은 텍스트 ""하나, 둘, phi ","거리 "'를 대체하기 때문에 파일의 이름은 변하지 않을 것이라고 기대할 수 있습니다. 또한 당신이 어떻게 왔는지 이해할 수 없습니다."name_file_-3_3_0_0 .txt "''-3 '은 어디에서 왔습니까? 귀하의 질문을 명확하게 편집하십시오. –
방금 템플릿에 대한 추가 정보를 추가했습니다. – Monica