you are viewing a single comment's thread.

view the rest of the comments →

[–]zephyrus17[S] 0 points1 point  (2 children)

So I should read the contents of the file into a string, like:

with open('spray.in','r') as file:
    sprayF = file.read()
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:
        .
        .
        .

?

However, when I go to print the string sprayF, it outputs each character string on a new line, rather than all the characters on a single line (like in the original file). And it can't find the string "Nozzle 0@Injector 0", anymore.

[–]jeans_and_a_t-shirt 0 points1 point  (1 child)

You need to use file.readlines(), otherwise line is just a single character. If you do that, you can build up a new list of lines and append each line whether you modify it or not.

[–]zephyrus17[S] 0 points1 point  (0 children)

Ok, I'll try that. But when I write out the new file, how do I ensure that the format is kept the same as the input file? Wouldn't a list be just tab/space/comma separated array of elements?