复制文本更改一些变量

时间:2014-02-21 10:34:00

标签: python file text

我想复制像这样的文本(它存储在txt文件中,比如 template.txt ):

//
// Pyrolysis of PMMA sample exposed to a 18 kW/m² cone irradiances
//
MODULE PEL_Application
   $DS_external_flux= 16.5e03 // 18 kW/m² en prenant en compte le transfert convectif.
   $DS_DX = 0.005
   $DS_activation_energy = 1.15e+5
   $DS_arrhenius_constant = 7.0e+6
   $DS_Kappa_s = 1000.
   $DS_initial_fuel_mass = 1.50272
   $DS_heat_of_pyrolysis = 2.0e+6
   $DS_T_initial = 304.75
   $DS_gas_refractive_index = 1.0
   $DS_medium_refractive_index = 1.0
END MODULE PEL_Application

#include ( join( "..", "ref.pel" ) )
MODULE PEL_Application
   MODULE PDE_DomainAndFields
      MODULE macro_boundary_conditions
         MODULE BC#inlet1
            MODULE BC#temperature
               type  = "volumetric_pyrolysis#semi_transparent_media"
            END MODULE BC#temperature
         END MODULE BC#inlet1
      END MODULE macro_boundary_conditions
   END MODULE PDE_DomainAndFields
END MODULE PEL_Application

我想替换

的值
   $DS_activation_energy = 1.15e+5
   $DS_arrhenius_constant = 7.0e+6

   $DS_activation_energy = new_value_1
   $DS_arrhenius_constant = new_value_2

并使用新值将整个文本复制到名为 data.txt 的新文件中。

请问如何在Python中完成?

提前谢谢

4 个答案:

答案 0 :(得分:0)

我希望这对你有用。

import fileinput
import sys

for line in fileinput.input('File.txt', inplace=1):
        if 'string_to_replace' in line:
                line = line.replace('string_to_replace','new_string')
        sys.stdout.write(line)

答案 1 :(得分:0)

这应该这样做。

f = open("template.txt", "r")
g = open("data.txt", "w")
lines = f.readlines()
new_value_1 = "Whatever"
new_value_2 = "Whatever else"
for each in lines:
    if each.strip().startswith("$DS_activation_energy"):
        split_val = each.split("=")
        split_val[-1] = new_value_1
        new_val = "= ".join(split_val) + "\n"
        g.write(new_val)
    elif each.strip().startswith("$DS_arrhenius_constant"):
        split_val = each.split("=")
        split_val[-1] = new_value_2
        new_val = "= ".join(split_val) + "\n"
        g.write(new_val)
    else:
        g.write(each)

答案 2 :(得分:0)

你可以做纪念ça:

import re

rgx = re.compile('(\A.+?\$DS_activation_energy[ \t]*=[ \t]*)'
                 '[^ \t\r\n]+'
                 '(.+?\$DS_arrhenius_constant[ \t]*=[ \t]*)'
                 '[^ \t\r\n]+'
                 '(.+\Z)',
                 re.DOTALL)

with open('template.txt','rb') as f:
    cont = f.read()

new_value_1,new_value_2 = 10, 892.12

with open('data.txt','wb') as f:
    f.write(rgx.sub('\\1%s\\2%s\\3',cont) % (new_value_1,new_value_2))

答案 3 :(得分:0)

这是一个利用“占位符”的解决方案。

template.txt

//
// Pyrolysis of PMMA sample exposed to a 18 kW/m² cone irradiances
//
MODULE PEL_Application
   $DS_external_flux= 16.5e03 // 18 kW/m² en prenant en compte le transfert convectif.
   $DS_DX = 0.005
   $DS_activation_energy = <activation_energy>
   $DS_arrhenius_constant = <arrhenius_constant>
   $DS_Kappa_s = 1000.
   $DS_initial_fuel_mass = 1.50272
   $DS_heat_of_pyrolysis = 2.0e+6
   $DS_T_initial = 304.75
   $DS_gas_refractive_index = 1.0
   $DS_medium_refractive_index = 1.0
END MODULE PEL_Application

#include ( join( "..", "ref.pel" ) )
MODULE PEL_Application
   MODULE PDE_DomainAndFields
      MODULE macro_boundary_conditions
         MODULE BC#inlet1
            MODULE BC#temperature
               type  = "volumetric_pyrolysis#semi_transparent_media"
            END MODULE BC#temperature
         END MODULE BC#inlet1
      END MODULE macro_boundary_conditions
   END MODULE PDE_DomainAndFields
END MODULE PEL_Application

现在,请阅读'template.txt'并替换值:

with open('template.txt') as input_file:
    template_text = input_file.read()

new_activation_energy = ...
new_arrhenius_constant = ...

new_text = template_text
new_text = new_text.replace('<activation_energy>', new_activation_energy)
new_text = new_text.replace('<new_arrhenius_constant >', new_arrhenius_constant)

with open('new_file.txt', 'w') as ouput_file:
    output_file.write(new_text)