Hi folks

I feel like this is a simple task, but being so new to python and not having the time to fully come to grips with it I am utterly overwhelmed...

I simply want to replace whatever text is at a particular position in a particular line with the value I have read from another file. For example (clearly wrong)

ifile = open('inputfile.txt','r')

#Read inputfile.txt which contains multiple lines with 2 cols of data, the 1st col contains the name of a variable & the 2nd col a new value to use such that eg. line[0]=('Variable1', 'new_value1'); line[1]=('Variable2', 'new_value2')

with open('ofile.txt', 'w') as outfile:
    with open(ifile, 'r') as infile:
        for line in infile:
            if line.startswith('Variable1'):
                outfile.write(#???write new_value1 to line x from position y to z of the line (incl blanks) ???)
                continue
            if line.startswith('Variable2'):
                outfile.write(#???somehow write new_value2 to line i from position j to k of the line (incl blanks) ???)
                continue

Helpies?
Thanksies!

Unfortunately none of those links are able to help me.

The problem is that the file I need to overwrite is a large text file full of numbers in particular places, corresponding to reads from the calling Fortran code, without any keywords to identify the variables. I have to refer to the user manual to deduce what line of the text file and what positions within that line correspond to the variable of interest.

The relative position of the variable's numeric value in the file is, fortunately, quite consistent, so if I can specify a point in the text file by using a line number and character position range on that line I can essentially treat the variable location as a cartesian point, with line number as y and character position as x1->x2.

I do not need to edit hundreds of variables, only a few, but I need to change the value of those few variables many times, so I can hard-code in the specific position I need to edit for each variable according to it's name. I could then read in the name as the first element and the value as the second element of the line:
e.g. line[0]=('Variable1', 'new_value1')

I would then check if the first value of the line is "Variable 1" and if it is then replace a particular location in the model's input file, based on line number and character position, with "new_value1"

I have NO clue.

If you need to change at places which are very specific and at are constant it is better you use regular expressions as the expression changes according to your data..

This is is the sample expressing I am passing you which may help you in getting an idea

(?<=imgfurl)(.*)(?=dim)

This is the expression to get all the data which appear to be constatnd between words imgfurl and dim

Like this write your own regular expression according to your data

Thank you for the suggestion, but unfortunately that solution won't work either.

I have attached a copy of the input file to illustrate exactly what I'm working with. I need to be able to replace any one of the numbers in that file.

I truly see no other option besides a line and position-on-the-line specification for a write statement.

I cannot perform any sort of search for a point of reference based on some word or non-numeric text because clearly there are none (there are some line labels but I can already specify a line - it's the line;line-position specification that is the problem). I also cannot perform a search for a particular number to replace because the numbers change all the time and can be repeated for different variables.

I looked at the file. It's a very nice file. Still I have no idea what should be replaced with what.
Can you please explain this, assuming that I am going to play a computer, that is, exhibit an intelligence slightly higher than one of a amoeba?

I looked at the file. It's a very nice file. Still I have no idea what should be replaced with what.
Can you please explain this, assuming that I am going to play a computer, that is, exhibit an intelligence slightly higher than one of a amoeba?

Right, so every single number in that file represents a parameter in my (BIG) computer model - it is an input file, and the position of the values in the text file is extremely important if they are to be read into the parameters correctly by the Fortran code my model is written in.

Some of the parameters in that file I would like to vary and then rerun the model to see the effect of changing the value. Which parameters I end up wanting to vary in particular don't matter because whatever method works for one should work for all.

I have a separate file containing the new values I would like to use (let's call it input2.txt). In order to use these values I have to know 1) what parameter the values in input2.txt correspond to, 2) where that parameter's position is in the main input file time.txt, and 3) how to replace the original value with the new value.

To solve 1) I have proposed that each line in input2.txt contain first a label (being the name of the parameter I want to replace) and a value (the new value I want to use in time.txt).

To solve 2) I would then use the label to identify which parameter it is. Knowing this I can use the model's user manual to determine exactly where in time.txt the new value must go: this information will be comprised of a line number, and a range within that line e.g. line 23, character position 20 to 25.

I do not know how to solve 3) - how do I write the confounded value I have read from input2.txt into the exactly correct position within time.txt?

Stubaan> I do not know how to solve 3) - how do I write the confounded value I have read from input2.txt into the exactly correct position within time.txt?
Let's get this last part first out of the way. You don't change it in place, you save it to a temporal buffer file and after finishing you overwrite the original.

Assuming your lines must keep same length after change. Assuming that everything is predictably formated.

For simplicity let say you have this file

Cool@Deepblue:~/Programming/Python$ cat time.txt

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

Now, to read this file line by line you might write:

while 1:
    line = inputfile.readline()  # grab a line
    if not line: break             # end of file reached
    # we do something with line now

If you want to know when you are in a particular line number, you can keep track of it and if you get to the wanted line you can perform modification on it:

lineno = 0                      # line tracker
while 1:
    line = inputfile.readline() # grab a line
    if not line: break              # end of file reached
    lineno = lineno + 1          # current working line
    
    if lineno == chosen_line:    # are we there yet?
        # Some work on given line will happen here
    else:
        # Copy as it is into temp file

Stubaan> To solve 1) I have proposed that each line in input2.txt contain first a label (being the name of the parameter I want to replace) and a value (the new value I want to use in time.txt).

Stubaan> To solve 2) I would then use the label to identify which parameter it is. Knowing this I can use the model's user manual to determine exactly where in time.txt the new value must go: this information will be comprised of a line number, and a range within that line e.g. line 23, character position 20 to 25.

OK. You need some values now and a couple files to work with:

parameterline = 2  # an arbitrary line to work on it; can be obtain from file

datamod = '-2.00'  # some data for modification, it can be obtain from a file as well
start_point = 19   # start_point = len('-1.00 -1.00 -1.00 ') 


inputfile = open('time.txt', 'r')

outputfile = open('time.txt.out', 'w')



lineno = 0              # line tracker

while 1:

    line = inputfile.readline()  # grab a line

    if not line: break             # end of file reached

    lineno = lineno + 1          # current working line

    if lineno == parameterline:     # are we there yet?

       # here's the working bit that makes the sustitution.

        modifiedline = line[:start_point] + datamod + line[start_point + len(datamod):]

        outputfile.write(modifiedline)

    else:

        outputfile.write(line)      # copy line as it is into temp file



inputfile.close()                   # done with it.

outputfile.close()                  # rename afterward with the original name

Cool@Deepblue:~/Programming/Python$ cat time.txt.out

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -2.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

-1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00 -1.00

Wow - Aia THANK YOU!

I need to absorb all of that and see where it gets me, but it definitely seems to be very much the sort of solution I am looking for.

I am currently swamped with other problems I need to be working on right now, having spent too long on this issue the past week, but hopefully some time in the coming week I can return to it and wrap it up, and then report back with good news.

My sincere thanks! And I really appreciate you taking the time to lay out the thought processes behind your proposed solution. Most of my recent Python frustrations stem from feeling like I am implementing code that I don't really understand because I do not have the luxury of time to study Python and work myself into it. This helps me learn and helps me - perfect!

Aia owe you big time!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.