Python Forum
How to print from all line in between two pattern
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to print from all line in between two pattern
#1
Sorry im totally new in python, if can have some explanation on code, and try split in few function, so i can more easily learn and apply at other place.
i'm try search net but unable to find this function, most of them can't extract exactly same , and can't extract out the line with ;.


Im have 2 file , file_A and file_B.

Have few condition.
1. Need exactly same only will extract out. exp(will only extract "+5VIN" pattern only , and "+5VIN_2" won't.)
2. extract all until found ";", include the line.
3. the output file will ignore "."
4. will remove duplicate.

file_A
#
+5VIN
2FS_ECL0
#

file_B
#
+5VIN
TP94.1
R1.2
U43.30;
+5VIN_2
TP95.1
U43.32;
2FS_ECL0
TP94.2
R100.1;
#

expected output file:
#
TP94
R1
U43
R100
#
Reply
#2
show what you have tried so far, and where the problem lies.
Reply
#3
(Feb-07-2019, 04:06 AM)Larz60+ Wrote: show what you have tried so far, and where the problem lies.

below is the code im try, but it unable to work, and it only hardcode , unable to get string from the other list.


Board_file = open("board","r")
Board_connection = open("Board_filter1","w")

def Board_File_filter1():
Beginning = "+5VIN"
Ending = ";"


print_swt = 0
with Board_file as f:
for line in f:
if Beginning in line:
print_swt = 1
if Ending in line:
print_swt = 0

if print_swt:
X = line
Board_connection.write(str(X))
Reply
#4
(Feb-07-2019, 04:06 AM)Larz60+ Wrote: show what you have tried so far, and where the problem lies.

i'm have try below code:

def Extract_part1():
   
    for Beginning in search_words:
        
        infile = open("File_B","r")
        outfile = open("Output_file","w")
            
        print_swt = 0
        with infile as f:
        
            for line in f:
                
                if Beginning in line:
                    print_swt = 1
                            
                if ";" in line:
                    print_swt = 0
                
                if print_swt:
                    X = line 
                    outfile.write(str(X))
                print(print_swt)

        infile.close()         
        outfile.close()


rmv_list = open("file_A")
search_words = []

for word in rmv_list:    
    search_words.append(word.strip())
rmv_list.close()            
the output file i get is(highlight in red only:+
Output:
+5VIN TP94.1 R1.2 U43.30; 2FS_ECL0 TP94.2 R100.1;
Reply
#5
I don't see where 'Extract_part1()' is being executed, please post runable code.
Reply
#6
(Feb-07-2019, 10:55 AM)Larz60+ Wrote: I don't see where 'Extract_part1()' is being executed, please post runable code.

i'm not a professional programmer , i'm just start learning to do some simple text processing job, hope some one can help on this, i'm try to search the part for few day but unable to find.


it run after rmv_list.close()

rmv_list = open("file_A")
search_words = []

for word in rmv_list:
search_words.append(word.strip())
rmv_list.close()

Extract_part1()

(Feb-07-2019, 10:55 AM)Larz60+ Wrote: I don't see where 'Extract_part1()' is being executed, please post runable code.

it run after rmv_list.close()


def Extract_part1():
    
    for Beginning in search_words:
         
        infile = open("File_B","r")
        outfile = open("Output_file","w")
             
        print_swt = 0
        with infile as f:
         
            for line in f:
                 
                if Beginning in line:
                    print_swt = 1
                             
                if ";" in line:
                    print_swt = 0
                 
                if print_swt:
                    X = line 
                    outfile.write(str(X))
                print(print_swt)
 
        infile.close()         
        outfile.close()
 
 
rmv_list = open("file_A")
search_words = []
 
for word in rmv_list:    
    search_words.append(word.strip())
rmv_list.close()
Extract_part1()            
Reply
#7
Hi All,

Im Need help, im been trying . below is my code, still have some error, which is it only extract 1st data only.

def Extract_part1():

    outfile = open("Board_filter3","w")
    infile = open("Board_filter2","r") 
    
    for line in infile:

        for Beginning in search_words:
            
            print_swt = 0
            for line in infile:
                                
                if Beginning == line.strip():
                    print(Beginning,print_swt)
                    print_swt = 1
                    continue
       
                if print_swt == 1:
                    X = line
                    outfile.write(X)
                    
                if ";" in line:
                    print_swt = 0
               
                    
    infile.close()         
    outfile.close()


rmv_list = open(rmv_list_input)
search_words = []

for word in rmv_list:
    
    search_words.append(word.strip())
rmv_list.close()            

Extract_part1()

     
Reply
#8
(Feb-07-2019, 03:18 AM)kttan Wrote: Have few condition.
1. Need exactly same only will extract out. exp(will only extract "+5VIN" pattern only , and "+5VIN_2" won't.)
2. extract all until found ";", include the line.
3. the output file will ignore "."
4. will remove duplicate.

(Feb-08-2019, 09:31 AM)kttan Wrote: Im Need help, im been trying . below is my code, still have some error, which is it only extract 1st data only.

Sorry, but these conditions don't make sense to me. If you cant articulate what are the exact conditions then it's near to impossible to help.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
Anda perlu menerangkan menggunakan Bahasa Melayu, kemudian pergi ke translate.google.com dan menterjemah ke bahasa inggeris sebelum posting. Dengan cara ini, anda akan dapat menyatakan dengan jelas persoalan anda.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with spliting line in print akbarza 3 375 Jan-23-2024, 04:11 PM
Last Post: deanhystad
  Print the line before the corrent line tester_V 9 1,563 Nov-18-2022, 08:39 AM
Last Post: Gribouillis
  Print to a New Line when Appending File DaveG 0 1,217 Mar-30-2022, 04:14 AM
Last Post: DaveG
  If match not found print last line tester_V 2 2,882 Apr-26-2021, 05:18 AM
Last Post: tester_V
  print a line break in writelines() method leodavinci1990 1 6,445 Oct-12-2020, 06:36 AM
Last Post: DeaD_EyE
  Print characters in a single line rather than one at a time hhydration 1 2,021 Oct-10-2020, 10:00 PM
Last Post: bowlofred
  How to print string multiple times on new line ace19887 7 5,722 Sep-30-2020, 02:53 PM
Last Post: buran
  Pattern Require Last Line Print() why? Harshil 4 2,421 Aug-08-2020, 04:54 PM
Last Post: Harshil
  print only last matched line tester_V 24 6,465 Apr-30-2020, 05:16 AM
Last Post: deanhystad
  item from a line to list however when i print the line instead of words i get letters Sutsro 5 2,946 Apr-22-2020, 02:39 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020