0

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.

Hafiz
  • 5
  • 1
  • 5

1 Answers1

0

I think it is not possible to use replace with an or statement.

I'll show two ways I could quickly think of to solve this:

  1. just call the replace statement twice
  2. use regular expressions

I created a dummy test.txt file myself and used the following two scripts:

from sys import argv
script, filename, sigma = argv

filename2 = "test2.txt"

with open(filename, 'r') as ft:
    txt = ft.read()

txt=txt.replace('3.7', sigma) 
txt=txt.replace('3', sigma)

with open(filename2, 'w') as ft:
    ft.write(txt)

And

from sys import argv
import re

script, filename, sigma = argv
filename2 = "test2.txt"

with open(filename, 'r') as ft:
    txt = ft.read()

txt_new = re.sub(r"3.7|3", sigma, txt)

with open(filename2, 'w') as ft:
    ft.write(txt_new)

I've rewritten your scripts a bit since I prefer to use

with open(filename, "r") as ft:
    txt = ft.read()

It is the same as:

file_data = open(filename, "r")
txt = file_data.read()
file_data.close()

But now I do not have to care about remembering the file_data.close() statement.

Note that I first replace 3.7 because if you first replace the string "3" than every "3.7" will become a "2.7" and therefore the replacement of "3.7" to 2 cannot happen.

This is also the reason why in the regular expression I wrote r"3.7|3" instead of r"3|3.7".

Please also note that this solves your question, but I'm not sure what the higher purpose of your script is so this method may in the end not be the best way to solve your problem.

Your second query fails because you wrote

txt=txt.replace('x="2"','x=sigma')

'x=sigma' is interpreted as a string and not as the variable sigma you defined. To solve this you could use something like this:

txt=txt.replace('x="2"', 'x=%s' % sigma)

The '%s' % variable statement will fill in the value of the variable sigma in the string. See this page

Good luck!

Community
  • 1
  • 1
DavedeKoning
  • 190
  • 1
  • 7
  • Thanks for your help. It works. To address: "I'm not sure what the higher purpose of your script is so this method may in the end not be the best way to solve your problem", I'm a new user of Python and following the way suggested by Zed Shaw. If you know better way, please share. I'd appreciate it. – Hafiz Apr 29 '17 at 13:50
  • You're welcome. I'm not familiar with Zed Shaw, but I like this page a lot! https://mbakker7.github.io/exploratory_computing_with_python/ – DavedeKoning Apr 30 '17 at 06:37
  • Well I was referring to a book for the beginner called "Learn Python the HARD Way". Anyway, thanks again for the link that you've shared. – Hafiz Apr 30 '17 at 11:04