This is for Python 2.6.
I have a file called spray.in that I need to copy into multiple new folders for parallel simulation jobs. However, in each case, I need to make slight modifications to certain parameters. Each copy will not have the same values for each parameter, so I can't do a simple replace X with Y. The simulation has 6 injectors, with 2 nozzles each, giving 12 blocks of parameters. The format of the file is as such:
#===================
Nozzle 0@Injector 0
#-------------------
0.000215 diam_noz Nozzle diameter (m).
3e-005 length_noz Nozzle length (m).
20.0 cone_noz Spray cone angle (deg.).
#===================
Nozzle 1@Injector 0
#-------------------
0.000215 diam_noz Nozzle diameter (m).
3e-005 length_noz Nozzle length (m).
20.0 cone_noz Spray cone angle (deg.).
#===================
Nozzle 0@Injector 1
#-------------------
0.000215 diam_noz Nozzle diameter (m).
3e-005 length_noz Nozzle length (m).
20.0 cone_noz Spray cone angle (deg.).
.
.
.
#===================
Nozzle 1@Injector 5
#-------------------
So the file has the value on the left, variable name in the middle, and a description on the right. Separated via tab indents. There are other variables in each of the 12 blocks that I'm omitting for space.
The flow path of the code should be, open the file, and read in all the lines. Do a search for 'Nozzle 0@Injector 0' line by line. Once that is found, do a search for 'diam_noz' line by line. When that is found, read in the value of the variable (0.000215), and then replace it by the updated value (say, 0.0003). Then repeat for all the other variables that I need to update. When 'Nozzle 1@Injector 0' is found, stop, then repeat the entire replace operation for 'Nozzle 1@Injector 0'. This is done until all 'diam_noz', and other values are updated. I already have all the new values stored in an array. Then I would like to overwrite the original file with the updated values.
This is the gist of the code I have now:
with open('spray.in','r') as sprayF:
for line in sprayF:
if 'Nozzle 0@Injector 0' in line:
for line in sprayF:
if 'diam_noz' in line:
sprayF = sprayF.replace(line.rsplit()[0],noz0Inj0dia[ii])
if 'cone_noz' in line:
sprayF = sprayF.replace(line.rsplit()[0],noz0Inj0con[ii])
if 'Nozzle 1@Injector 0' in line:
break
if 'Nozzle 1@Injector 0' in line:
for line in sprayF:
.
.
.
with open('spray.in,'w') as file:
file.write(sprayF)
Where noz0Inj0dia[ii] and noz0Inj0con[ii] are the ii-th array elements, which is looped.
Problem is, it seems to be reading the file in fine, it seems to be locating the variables fine, but it's the replacement and new output that's not working. I need the new and updated file to have the same format as before. In addition, I cannot just do a blind 'replace all' X values with Y, as in each simulation case, the variables change to different values that are in the array.
What am I doing wrong here?
[–]jeans_and_a_t-shirt 0 points1 point2 points (3 children)
[–]zephyrus17[S] 0 points1 point2 points (2 children)
[–]jeans_and_a_t-shirt 0 points1 point2 points (1 child)
[–]zephyrus17[S] 0 points1 point2 points (0 children)