1

I'm trying to loop through a file and replace all instances of several things.. It's working SOME of the time, but it's missing values that it correctly replaced earlier in the file and I have no idea why it won't work every time.

with open("myfile", "rt") as filein:
with open("mynewfile", "wt") as fileout:
    for line in filein:
        fileout.write(line.replace('a', 'b'))
        fileout.write(line.replace('c', 'd'))
        fileout.write(line.replace('e', 'f'))
        fileout.write(line.replace('g', 'h'))
        fileout.write(line.replace('i', 'j'))
        fileout.write(line.replace('k', 'l'))
print("Done")
1
  • The replace method does not mutate the string it returns a new one Commented Jan 3, 2018 at 0:05

3 Answers 3

1

The replace method does not mutate the string it returns a new one
try this

with open("myfile", "rt") as filein, open("mynewfile", "wt") as fileout
        for line in filein:
            line = line.replace('a', 'b')
            fileout.write(line)
            line = line.replace('c', 'd')
            fileout.write(line)
            ...

or better

list_replace = [('a', 'b'), ('c', 'd'), ...]
with open("myfile", "rt") as filein, open("mynewfile", "wt") as fileout
        for line in filein:
            for t in list_replace:
                line = line.replace(*t)
            fileout.write(line)

Update based on the comments

list_replace = [('a', 'b'), ('c', 'd'), ...]
with open("myfile", "rt") as filein, open("mynewfile", "wt") as fileout
        for line in filein:
            tmp = line[36:]
            for t in list_replace:
                tmp = tmp.replace(*t)
            fileout.write(line[:36] + tmp)
Sign up to request clarification or add additional context in comments.

11 Comments

I did not get it , could please explain more
like this, so it's replacing SOME of the values but missing others even if it caught it earlier line1: original line2: original, original line3: original, replaced line4: replaced, replaced line5: replaced
it looks like it worked!! thank you so much! what did that change do? it looks like all you did was move the 2nd open to the same line as the first, is there a way to make it start looking to replace at a certain point in the line? there is an ID before the part i'm replacing and it looks like the numbers i'm replacing are in some of those ids
Yep, before it writes the line after each replace operation
It works perfectly, you rock thank you so much for all the help
|
0

Cannot replicate your problem in Python 3. The following works just fine:

with open("myfile", "rt") as filein, open("mynewfile", "wt") as fileout:
    for line in filein:
        fileout.write(line.replace('a', 'b'))
        fileout.write(line.replace('c', 'd'))
        fileout.write(line.replace('e', 'f'))
        fileout.write(line.replace('g', 'h'))
        fileout.write(line.replace('i', 'j'))
        fileout.write(line.replace('k', 'l'))
print("Done")

You have to account for the presence of a \n at the end of filein though, so check for it.

Comments

0

You're writing out 6 lines to fileout for every line you read in from filein. Each new line only replaces 1 character, not all of them.

If you're trying to replace everything in the line, try the following. multi_replace takes a list of search strings, and replace strings, then iterates over them to replace them individually on each line.

def multi_replace(line, searchlist, replacelist):
   for i, j in zip(searchlist, replacelist):
     line = line.replace(i, j)
   return line

with open("myfile", "rt") as filein:
  with open("mynewfile", "wt") as fileout:
    for line in filein:
        fileout.write(multi_replace(line, ['a','c','e','g','i','k'],['b','d','f','h','j','l']))
print("Done")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.