1

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)
JR Santos
  • 137
  • 1
  • 11
  • Try using `with open(r"file1.txt", 'r+')` or `with open(r"file1.txt", 'w+')` to read and write. I'm not sure if the indentation became weird when you posted the question, but the line `with open(r"file1.txt", 'r') as file:` doesn't do anything currently. More info about `w+`, `r+` here: https://stackoverflow.com/questions/1466000/difference-between-modes-a-a-w-w-and-r-in-built-in-open-function – Jhanzaib Humayun Apr 23 '22 at 04:11
  • 1
    Is there a rule for each code? Can't you use regular expressions? – Fabio Craig Wimmer Florey Apr 23 '22 at 04:18
  • @JhanzaibHumayun - I did try that and still nothing I add the `+` I did get something that works, but I can't get it to find multiple rows - please see my question for the updated script – JR Santos Apr 23 '22 at 04:33
  • @FabioCraigWimmerFlorey - the file is a Json converted to txt and I have different codes, and all I need it to do is find the code1 and delete the whole row, my last update that I shared works, but only one code, need help making read multiple codes – JR Santos Apr 23 '22 at 04:39
  • @JhanzaibHumayun - I see what you mean about the first `with` I moved it around and still nothing – JR Santos Apr 23 '22 at 04:47
  • @FabioCraigWimmerFlorey - I'm no good with regular expressions - I still can't figure out how to overwrite old data with new for example `"code1": ??,` gets replaced with `"code1": 100" ` if I can get something like that to replace random numbers with a number of my choice that would work as well – JR Santos Apr 23 '22 at 04:50
  • I'd never used the `f.seek(0)` `f.truncate()` method before, but it was brilliant. I implemented it into my code below, which constructs a simple regex `or` statement out of a list of words, and then is used to filter out lines from the document. – BeRT2me Apr 23 '22 at 04:51
  • @Hot-Topic I gave you a response in the thread with the code for a given rule, for a list of codes or for a range of codes. Check those out. – Fabio Craig Wimmer Florey Apr 23 '22 at 05:12

3 Answers3

0

Given Input File:

"nman": "afklafjlka"
"prvr": "521.0.25",
"prvrfi": "1.18.3",

| acts like a boolean OR in regex

import re

words = ['"nman"', '"prvr"'] # USE THE '""' OR YOU MAY DELETE MORE THAN EXPECTED
words = '|'.join(words) # == "nman"|"prvr"
with open('test.txt', 'r+') as f:
  # Marker used by truncate.
    f.seek(0)
  # Adds lines to the list if a regex match is NOT found. 
  # Matching the OR statement we created earlier.
    lines = [x for x in f.readlines() if not re.match(words, x)]
  # Writes all the lines we found to the file.
    f.writelines(lines)
  # Cuts off everything we didn't add.
    f.truncate()

Output:

"prvrfi": "1.18.3",
BeRT2me
  • 12,699
  • 2
  • 13
  • 31
  • Please add some description in addition to code. – keiv.fly Apr 23 '22 at 14:31
  • @BeRT2me - can you take a look at my updated question, none of the codes worked How does this work `words = ['a', 'b']` and `words = '|'.join(words)` because nothing tat was given to me worked – JR Santos Apr 23 '22 at 20:55
  • @Hot-Topic Made some edits with lots of comments. You can check out this [regexr](https://regexr.com/6k77e) for more visual view of what's happening with the regex. – BeRT2me Apr 23 '22 at 21:47
  • Thank you it doesn't work, not sure if indents matter or not but I have 4 white spaces in front of each code, and others might have 5, I tested and nothing happened, I removed the indents and it cloned everything in the file, so now I have double / repeated codes – JR Santos Apr 24 '22 at 15:19
0
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)
chongkai Lu
  • 452
  • 3
  • 9
  • Thank you, I really thought yours was going to work but it failed, please look at my updated question – JR Santos Apr 23 '22 at 20:56
  • @Hot-Topic I got it. You want to delete a whole row if it **contains** one of the substings. The answer is updated. It now requires a two-depth nested loop, which I think is inevitable. – chongkai Lu Apr 24 '22 at 03:51
  • Perfect - I add these to control it's finding ` ' '` like this `to_delete = ['"nman"', '"prvr"']` because it would delete anything with the combination of the codes, I do have a question is there a way to add the codes going straight down, like a list – JR Santos Apr 24 '22 at 15:29
  • Do you mean `new_lines = ["" if d in l else l for l in lines for d in to_delete]`? The problem is that it cannot break the loop in advance, and actually a nested loop in list comprehension is no longer beautiful. – chongkai Lu Apr 24 '22 at 15:35
  • Thank you I figured out the list would like your help with 1 more question - it's going to be with the same script, because it's with the same patterned idea, but this will have this `"prvr": {` or `"prvr": [` – JR Santos Apr 24 '22 at 15:58
  • I added the final script to my question, so you can see the list idea I have, instead of it going straight across it goes straight down, I'm about to post my new question if you can help me with that, that would be awesome – JR Santos Apr 24 '22 at 16:07
  • I have posted my other question, if you can take a look at that, I would really appreciate – JR Santos Apr 24 '22 at 16:23
0

If there's a rule you can delete rows in a single wipe with regular expressions.

This one deletes all the rows that start with "code followed by a number.

from re import sub

with open('file1.txt', 'r') as f:
        text = f.read()
    
with open('file1.txt', 'w') as f:
    f.write(sub('("code\d+".*(\n|))','',text))

If there's a rule but you don't want to use RegEx or you don't know how to use them, then you can use a function to check if a row is good or bad

from re import search

code_list = ['1', '2', '3']

with open('file1.txt', 'r') as f:
        text = f.readlines()
    
with open('file1.txt', 'w') as f:
    f.writelines([_ for  _ in text if not any([search(f'code{i}', _) for i in code_list])])

Or within a range:

from re import search

with open('file1.txt', 'r') as f:
        text = f.readlines()
    
with open('file1.txt', 'w') as f:
    f.writelines([_ for  _ in text if not any([search(f'code{i}', _) for i in range(100)])])
  • Thank but none worked the script needs to find KEY words/letters, I have over 100 different codes all codes have there own name – JR Santos Apr 23 '22 at 20:39