Query 1: I have a .txt file and I want to replace two numbers say 3.7 and 3.5 with 2. It means, I need to search 3.7 or 3.5 and replace them by 2. I want to do it by passing argument to the script. I can replace it for a single number as:
#####test.py##########
from sys import argv
script,filename,sigma = argv
file_data = open(filename,'r')
txt = file_data.read()
txt=txt.replace('3.7',sigma)
file_data = open(filename,'w')
file_data.write(txt)
file_data.close()
It's run in command line with test.txt as
python test.py test.txt 2
Now I want to extend it with logic OR as:
#####test.py##########
from sys import argv
script,filename,sigma = argv
file_data = open(filename,'r')
txt = file_data.read()
txt=txt.replace('3.7'|'3',sigma) #gives syntax error
file_data = open(filename,'w')
file_data.write(txt)
file_data.close()
The above implementation gives syntax error.
Any suggested modification?
Query 2: I'm rewriting it in the following manner:
#####test.py##########
from sys import argv
script,filename,sigma = argv
file_data = open(filename,'r')
txt = file_data.read()
txt=txt.replace('x="2"','x=sigma')
file_data = open(filename,'w')
file_data.write(txt)
file_data.close()
With
python test.py test.txt 3.
I get x=sigma, but I want to get x=3
What'd be the modification?
Your comments/feedback are highly appreciated.
Hafiz.