简体   繁体   中英

Python: Remove substrings with fixed start and end from .txt file

I have some file p2.txt of following content:

%         proof/6: [all]
 T [14] Call: proof(([p∨q∨r], [])⊢(q∨p∨r), _16004, _16006, proof{
    1: ["p∨q∨r","step(1)"]

}, _16010, _16012)
 T [24] Call: proof(([temp(p∨q∨r)], [])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)), _16004, _16006, proof{
    0: ["assumptions([p∨q∨r])","premisses_origin([p∨q∨r,(p→q∨p∨r)∧(q∨r→q∨p∨r)])","premisses_no_origin([])","premisses_exc_origin([])","conclusion([q∨p∨r])","rule([∨E])","d0(([p∨q∨r],[])⊢(q∨p∨r))","d1(([temp(p∨q∨r)],[])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)))","step(2)"]

    1: ["p∨q∨r","step(1)"]

}, _16010, 1)
 T [34] Call: proof(([temp(p∨q∨r)], [])⊢(p→q∨p∨r)∧([temp(p∨q∨r)], [p→q∨p∨r])⊢(q∨r→q∨p∨r), _16004, _16006, proof{
    -1: ["assumptions([p∨q∨r])","premisses_origin([p→q∨p∨r,q∨r→q∨p∨r])","premisses_no_origin([])","premisses_exc_origin([])","conclusion([(p→q∨p∨r)∧(q∨r→q∨p∨r)])","rule([∧I])","d0(([temp(p∨q∨r)],[])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)))","d1(([temp(p∨q∨r)],[])⊢(p→q∨p∨r)∧([temp(p∨q∨r)],[p→q∨p∨r])⊢(q∨r→q∨p∨r))","step(3)"]

    0: ["assumptions([p∨q∨r])","premisses_origin([p∨q∨r,(p→q∨p∨r)∧(q∨r→q∨p∨r)])","premisses_no_origin([])","premisses_exc_origin([])","conclusion([q∨p∨r])","rule([∨E])","d0(([p∨q∨r],[])⊢(q∨p∨r))","d1(([temp(p∨q∨r)],[])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)))","step(2)"]

    1: ["p∨q∨r","step(1)"]

}, _16010, 0)
 T [43] Call: proof(([temp(p∨q∨r)], [])⊢(p→q∨p∨r), _31686, _31688, proof{
    -1: ["assumptions([p∨q∨r])","premisses_origin([p→q∨p∨r,q∨r→q∨p∨r])","premisses_no_origin([])","premisses_exc_origin([])","conclusion([(p→q∨p∨r)∧(q∨r→q∨p∨r)])","rule([∧I])","d0(([temp(p∨q∨r)],[])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)))","d1(([temp(p∨q∨r)],[])⊢(p→q∨p∨r)∧([temp(p∨q∨r)],[p→q∨p∨r])⊢(q∨r→q∨p∨r))","step(3)"]

    0: ["assumptions([p∨q∨r])","premisses_origin([p∨q∨r,(p→q∨p∨r)∧(q∨r→q∨p∨r)])","premisses_no_origin([])","premisses_exc_origin([])","conclusion([q∨p∨r])","rule([∨E])","d0(([p∨q∨r],[])⊢(q∨p∨r))","d1(([temp(p∨q∨r)],[])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)))","step(2)"]

    1: ["p∨q∨r","step(1)"]

}, _31664, _31694)
 T [53] Call: proof(([temp(p∨q∨r), p], [])⊢(q∨p∨r), _31686, _31688, proof{
    -2: ["assumptions([p,p∨q∨r])","premisses_origin([q∨p∨r])","premisses_no_origin([])","premisses_exc_origin([p])","conclusion([p→q∨p∨r])","rule([→I])","d0(([temp(p∨q∨r)],[])⊢(p→q∨p∨r))","d1(([temp(p∨q∨r),p],[])⊢(q∨p∨r))","step(5)"]

    -1: ["assumptions([p∨q∨r])","premisses_origin([p→q∨p∨r,q∨r→q∨p∨r])","premisses_no_origin([])","premisses_exc_origin([])","conclusion([(p→q∨p∨r)∧(q∨r→q∨p∨r)])","rule([∧I])","d0(([temp(p∨q∨r)],[])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)))","d1(([temp(p∨q∨r)],[])⊢(p→q∨p∨r)∧([temp(p∨q∨r)],[p→q∨p∨r])⊢(q∨r→q∨p∨r))","step(3)"]

    0: ["assumptions([p∨q∨r])","premisses_origin([p∨q∨r,(p→q∨p∨r)∧(q∨r→q∨p∨r)])","premisses_no_origin([])","premisses_exc_origin([])","conclusion([q∨p∨r])","rule([∨E])","d0(([p∨q∨r],[])⊢(q∨p∨r))","d1(([temp(p∨q∨r)],[])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)))","step(2)"]

    1: ["p∨q∨r","step(1)"]

    2: ["p","step(4)"]

}, _31664, _32382) ...

my aim is to convert it into some file p2_red.txt , which looks like this:

%         proof/6: [all]
 T [14] Call: proof(([p∨q∨r], [])⊢(q∨p∨r), _16004, _16006, _16010, _16012)
 T [24] Call: proof(([temp(p∨q∨r)], [])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)), _16004, _16006, _16010, 1)
 T [34] Call: proof(([temp(p∨q∨r)], [])⊢(p→q∨p∨r)∧([temp(p∨q∨r)], [p→q∨p∨r])⊢(q∨r→q∨p∨r), _16004, _16006, _16010, 0)
 T [43] Call: proof(([temp(p∨q∨r)], [])⊢(p→q∨p∨r), _31686, _31688, , _31664, _31694)
 T [53] Call: proof(([temp(p∨q∨r), p], [])⊢(q∨p∨r), _31686, _31688, , _31664, _32382)

to solve this I wrote following code:

import re

with open("p2.txt") as f:
    content = f.read()


result = re.sub(", proof{.*?}",'', content)

print(result)

with open("p2_red.txt", "w") as f:
    f.write(result)

but for some reason this does not work. How can I fix that code?

Found some solution (its a little bit complicated, but probably their is no more direct way):

def remove_substrings_from_file(file_read, file_write, start, end):
    '''
        Remove substrings with fixed 'start' and 'end' from .txt file 'file_read' and saves it into 'file_write'.
        more details see:
         https://stackoverflow.com/questions/72931747/python-remove-substrings-with-fixed-start-and-end-from-txt-file 
    '''
    with open(file_read) as f_read:
        with open(file_write, "w") as f_write:
            append_to_file = True
            
            for s in f_read:
                if s.find(start) != -1:
                    if append_to_file == False:
                        raise Exception("Term 'proof' in 'proof' at line " + s)
                    
                    append_to_file = False
                    i = s.index(start)
                    f_write.write(s[0:i - 1])
                elif s.find(end) != -1:
                    if append_to_file == True:
                        raise Exception("Term '}' outside of 'proof' at line " + s)

                    append_to_file = True
                    i = s.index(end)
                    f_write.write(s[i : -1] + "\n")
                else:
                    if append_to_file == True:
                        f_write.write(s)


start = ", proof{"
end = "}"

file_read = "p2.txt"
file_write = "p2_red.txt"


remove_substrings_from_file(file_read, file_write, start, end)

output:

%         proof/6: [all]
 T [14] Call: proof(([p∨q∨r], [])⊢(q∨p∨r), _16004, _1600}, _16010, _16012)
 T [24] Call: proof(([temp(p∨q∨r)], [])⊢((p→q∨p∨r)∧(q∨r→q∨p∨r)), _16004, _1600}, _16010, 1)
 T [34] Call: proof(([temp(p∨q∨r)], [])⊢(p→q∨p∨r)∧([temp(p∨q∨r)], [p→q∨p∨r])⊢(q∨r→q∨p∨r), _16004, _1600}, _16010, 0)
 T [43] Call: proof(([temp(p∨q∨r)], [])⊢(p→q∨p∨r), _31686, _3168}, _31664, _31694)
 T [53] Call: proof(([temp(p∨q∨r), p], [])⊢(q∨p∨r), _31686, _3168}, _31664, _32382

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM