Python Forum
Parsing file and get a specific text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing file and get a specific text
#1
Hi everyone,

I'm new in scripting with python (in general too). I need some help to get a block text in a file.

This is my code :
stop = "end"
last_line = 0
searchtxtDetected = False
with open("20181107-115829-192.168.220.201.txt", 'r') as file:
	for txt in file:
		if 'show running-config' in txt:
			searchtxtDetected = True
			continue
		if searchtxtDetected:
			print (txt)
			#last_line += 1
			if txt == stop:
				break
Nevertheless, the code start when show running-config is parse but dont stop when it match my variable and print the entire file end !

The file to check my code
https://ufile.io/4eiaa
Reply
#2
When you loop through the lines in a file like that, you will have a newline character at the end of each line. Try changing the last comparison to if txt.strip() == stop:, or setting stop = 'end\n'.

Also, file is a built-in type in Python. If you use it as a variable name, it might mess up other code that depends on it.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Use stop = "end\n"
Reply
#4
And reverse the two if statements to get rid of the continue
    for txt in file:
        if searchtxtDetected:
            print (txt)
            #last_line += 1
            if txt == stop:
                break
        if 'show running-config' in txt:
            searchtxtDetected = True
Also, a break and/or continue statement is unreliable IMHO for nested loops. Especially if the code is modified in the future and someone adds another nested loop. It is better to use a function, and return from the function instead of the break.
Reply
#5
Hi,

Thanks for your replies !

I've tested your solution and the result is ok for the entire solutions :D

I'm going care about your advices and making better my code.

Thanks you everyone.

dds69
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Text parsing Arik 5 403 Mar-11-2024, 03:30 PM
Last Post: Gribouillis
  Extracting specific file from an archive tester_V 4 517 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Color a table cell based on specific text Creepy 11 1,993 Jul-27-2023, 02:48 PM
Last Post: deanhystad
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,892 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,127 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Reading Specific Rows In a CSV File finndude 3 990 Dec-13-2022, 03:19 PM
Last Post: finndude
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,281 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Modify values in XML file by data from text file (without parsing) Paqqno 2 1,675 Apr-13-2022, 06:02 AM
Last Post: Paqqno
  Parsing xml file deletes whitespaces. How to avoid it? Paqqno 0 1,042 Apr-01-2022, 10:20 PM
Last Post: Paqqno
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,998 Mar-28-2022, 03:38 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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