I am trying to write a python script that can take a word from one file and transfer it to a specific location in another file.
for example, lets say i have a file called hb15b.txt
and it includes the following:

MODIFY-USER 2671, "hb15b01", " UGz9", 1,

How can i get the position of hb15b01 and then use the position to transfer hb15b01 to another text file?

i know i am a beginner and still need guidance from those who know more than i do but i still would like to kind of figure out the solution myself. don't like to be spoon fed!

8 Years
Discussion Span
Last Post by woooee

Do you have to maintain the position in the second file?
What would a line of data in that file look like?


Yes, i do have to maintain the position in the second file.
The second file is just a table with some text. Each cell in the table has text written in this format:
cell1: hb5a01 cell2:hb5a00 cell3:hb5a03
rF6OLOh 9WKxKu 181fKm and so on


Generally, you would do a merge type program. Read in both files, compare and change, then write to a third file, which will be the merged file. So some pseudo-code

if "hb15b01" in record, find record in second file to change (let's say it has "hb15b00"), change "hb15b00" to "hb15b01" and write to the thiird file, then rinse and repeat.

It is not a good idea to try and replace directly into a file, because everything has to be exactly the same, and you can't guarantee that with any file. If you are replacing
2671, "hb1", " UGz9", 1,
a direct overlay could wind up with something like
2671, "hb15b01UGz9", 1,
because of the different lengths, which is not what you want. So you would generally use split() to isolate the field you want to change, modify it, and then use join() to convert back to a string.

Votes + Comments
good point
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.