I had to redo my questions because it made everyone focus on the wrong word
Sorry about this guys but I did put that I have 100 rows with different code names
This is my working code
with open("file1.txt","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "nman" not in line:
f.write(line)
f.truncate()
inside the text file
Before = file1.txt
"nman": "afklafjlka"
"prvr": "521.0.25",
"prvrfi": "1.18.3",
RESULTS = file1.txt
"prvr": "521.0.25",
"prvrfi": "1.18.3",
As you can see in my result the code "nman" was removed the whole row was removed
I made something in batch for this, but it's way to slow
I used in batch this script
findstr /V "\<nman\> \<prvr\>" file1.txt > file2.txt
So my end result for the updated script should be able to read many different code names just like my batch script
with open("file1.txt","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if "nman" "prvr" not in line: < --------
f.write(line)
f.truncate()
or something like this
to_delete = ["nman", "prvr"] < ------
with open("file1.txt","r+") as f:
new_f = f.readlines()
f.seek(0)
for line in new_f:
if to_delete not in line: < --------
f.write(line)
f.truncate()
Working Script Thank you
with open("file1.txt", 'r') as f:
lines = f.readlines()
to_delete = ["nman",
"prvr"]
new_lines = []
for l in lines:
for d in to_delete:
if d in l:
l = ""
break
new_lines.append(l)
with open("file2.txt", 'w') as file2:
file2.writelines(new_lines)