[Answer]-Line replace two values python django

1πŸ‘

βœ…

The solution is:

with open('/home/usr/dev/Django/rulebase/test_pattern_tmp.rb', 'r') as pattern_reading:
    with open('/home/usr/dev/Django/rulebase/test_pattern.rb', 'w') as pattern_writing:
        for line in pattern_reading:
            out=line.replace('pattern="Default"','Now').replace('input="Default"','This')
            pattern_writing.write(out)

Hi, there are two issues:

  1. you are reading the whole file into memory (which is not necessarily an error)
  2. A file object may be considered as an iterator. As soon as you have reached the end of file in your first iteration the secon iteration does nothing
πŸ‘€ProfHase85

0πŸ‘

Its writing one value because your lines list only contains the last replacement; you need to loop over lines in the second statement where you replace input = "Default".

πŸ‘€Burhan Khalid

Leave a comment