Python 替换文件中的变量对

Python 替换文件中的变量对,python,Python,我正在处理一个问题,我的目标是替换文件中的变量和文件名。 问题是我必须同时为所有组合(通常为24个组合)更改两个变量 我知道如何创建字符串的所有组合,但我想将列表放入其中并对其进行迭代 a = [ 'distance', 'T1', 'T2', 'gamma' ] new_list = list(itertools.permutations(a, 2)) 我创建了传递我的值的函数: def replace_variables(distance ='0', T1 ='0', T2 = '0',

我正在处理一个问题,我的目标是替换文件中的变量和文件名。 问题是我必须同时为所有组合(通常为24个组合)更改两个变量

我知道如何创建字符串的所有组合,但我想将列表放入其中并对其进行迭代

a = [ 'distance', 'T1', 'T2', 'gamma' ]
new_list = list(itertools.permutations(a, 2))
我创建了传递我的值的函数:

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
当我调用这个函数时,我只能传递变量的名称

for i in new_list:
        elem1 = i[0]
        elem2 = i[1]
        template_new, input_file =replace_variables(elem1, elem2)
        print input_file
虽然我需要使用列表:

distance = ['-3','+3']
T1 = ['-3', '+3']
T2 = ['-3', '+3']
gamma = ['-3', '+3']
对于每对变量,更改文件中的值和文件名,例如:

原始文件:
name\u file\u R\u T1\u T2\u gamma.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_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

"""

等等

我想我几乎解决了上述问题:

#!/usr/bin/env python
import itertools
import copy

def replace_variables(i, distance ='0', T1 ='0', T2 = '0', gamma = '0' ):
        k_  = copy.deepcopy(i)
        k_[0][0] = '-2'
        k_[1][0] = '2'
        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('gamma','gamma'+gamma).replace('distance','R'+distance)

     f = open(template_new, 'w')
     f.write(template_new)
     f.close()


input_name = 'name_file_distance_T1_T2_gamma.txt'

template = """
  R              =         3.0 *distance* cm
 THETA1         =         60. *T1*  degree
 THETA2         =         2.0  *T2* degree
 GAMMA          =         0 *gamma* degree
"""

a = [['distance','+2','-2'], ['T1','+2','-2'], ['T2','+2','-2'], ['gamma','+2','-2']]
new_list = list(itertools.permutations(a, 2))

for i in new_list:
      replace_variables(i, x, y)
虽然我面临两个问题:

1) 我的代码不会更改replace_variables函数中变量的值(默认值除外),我得到: 名称\u文件\u Rdistance\u T1T1\u T20\u gamma0.txt,依此类推 我认为是因为传递给函数的默认参数


2) 我的功能不会创建单独的文件。

您的帖子不清楚。如果您提供有关模板的更多信息,也许可以。那么
输入\u name=“name\u file\u R\u T1\u T2\u gamma.txt”
?您替换了文本
“一”、“二”、“φ”、“距离”
,因此我希望文件名保持不变,而且我不明白您是如何产生
“name\u file\u3\u 0\u 0.txt”
-3
从何而来?请编辑以澄清您的问题。我刚刚添加了有关我的模板的更多信息。