I'm writing a code in which I'm trying to negate certain arguments. For example, I want to pull in data from an inFile and search for "has" and replace it with "has not" and find "has not" and replace it with has. I have written a code that successfully does this if only one or the other argument is found, but when both arguments are in the same line, I am reaching a problem.
What I would like to attempt is to search the line for both words and their index values, create an if statement saying if one value is less than another (finding which one appears first), to slice the string, negate the portion I just sliced, and add it to an empty string, and continue the process until all the arguments are gone. I don't have too great of an idea on how to execute this (I am a beginner). The following code is what I've written, but it's incomplete.
The section where I am attempting to find, splice, replace, and move on, is bringing up several errors. I have a feeling that there is a much better way to go about doing this, and many thanks in advance for any help.
inFile = inFile.readlines()
#this is where I'm attempting to find, slice and replace
#I got a tip to use the "has not" == "has not" and "has"=="has" that way when searching for has, it only brings a result when it matches "has" exactly, instead of finding "has not"
for line in inFile:
newline = ""
while "has not" == "has not" and "has" == "has" in line:
x = line.find(line, (has" == "has"))
y = line.find(line, ("has not" == "has not"))
if x < y:
line2 = line[:x]
line2 = line2.replace("is" == "is", "is not")
newline = newline + line2
line3 = line[:x]
line3 = line3.replace(("has not" == "has not"), "has")
newline = newline + line3
#these two statements work if only one or the other are in a lien
if "has not" == "has not" in line:
line = line.replace("has not", "has")
elif "has" == "has" in line:
line = line.replace("has" , "has not")