Line replace two values python django -
i want line replace 2 values in same file. want search 'input="default"' , replace value 'this' , pattern="default" 'now'. have created tmp form code reads values , writes file.am new django , python.at moment writing 1 value.
views.py
with open('/home/usr/dev/django/rulebase/test_pattern_tmp.rb', 'r') pattern_reading: lines = [line.replace('pattern = "default"','now') line in pattern_reading.readlines()] lines = [line.replace('input = "default"','this') line in pattern_reading.readlines()] open('/home/usr/dev/django/rulebase/test_pattern.rb', 'w') pattern_reading: pattern_reading.writelines(lines)
the solution is:
with open('/home/usr/dev/django/rulebase/test_pattern_tmp.rb', 'r') pattern_reading: open('/home/usr/dev/django/rulebase/test_pattern.rb', 'w') pattern_writing: line in pattern_reading: out=line.replace('pattern="default"','now').replace('input="default"','this') pattern_writing.write(out)
hi, there 2 issues:
- you reading whole file memory (which not error)
- a file object may considered iterator. have reached end of file in first iteration secon iteration nothing
Comments
Post a Comment