python


print the string in between 2 conditions of text file in Python


I am new to python.I am struggling to print the string in between conditions of text file.My text file contains information below
03:37:59 CONFIRM 03.37.59 IS-0008 SS-XYZ SSU-BSS AB-008966 CTL-I001234
03:37:59 123HTSA TVP5
08:12:19 SNAP 290D 08.12.20 CX-A TU-BSS ZRE-BSS IS-3
08:12:19 PSW-07151000 813264 00000000 2967561E
08:12:19 PGM-BA13 CODE-U00BA1302 TERM-000000A
/*After this a few lines of data display*/
08:12:19 GOOD BYE TAKE CARE
Third Line has “SNAP” and “IS-“
Line 5 has “PGM-XXXX” where XXXX = program name and “CODE-XXXXXXXXX” XXXXXXXXX = type of Snap dump as coded.
This will be followed by a few lines of data display. The number of lines is variable and can change.
The last line will be “GOOD BYE TAKE CARE”
My output should be
08:12:19 SNAP 290D 08.12.20 CX-A TU-BSS ZRE-BSS IS-3
08:12:19 PSW-07151000 813264 00000000 2967561E
08:12:19 PGM-BA13 CODE-U00BA1302 TERM-000000A
/*After this a few lines of data display*/
08:12:19 GOOD BYE TAKE CARE
I have tried but i want to print upto 08:12:19 GOOD BYE TAKE CARE
with open("TEXT.txt", "r") as file:
output = ""
for line in file.readlines():
if "CTL" in line:
output += line
print(output)
printing = False
with open('text.txt') as file:
for line in file.readlines():
if line[9:13] == 'SNAP':
printing = True
if printing:
print (line.strip())
if line == '08:12:19 GOOD BYE TAKE CARE':
break
Initially the Boolean variable printing is set to False so that printing is disabled. Lines are read in a loop. When a line is read with 'SNAP' in the special location printing is set toTrue` to enable printing. In this condition lines are printed as soon as they are read. When a line is read containind 'GOOD BYE [etc]' the loop is broken and the script comes to an end.
EDIT: If you want to end printing prior to 'GOOD BYE [etc]' then the script could be the following.
printing = False
with open('text.txt') as file:
for line in file.readlines():
if line[9:13] == 'SNAP':
printing = True
if line.strip() == '08:12:19 GOOD BYE TAKE CARE':
break
if printing:
print (line.strip())
Notice especially that line must be stripped of end-of-line characters to test for equality.
If I understand you correctly that you just need to print lines staring with the line containing SNAP and IS-, then how about this?
import re
with open("TEXT.txt", "r") as file:
output = ""
ok = False
for line in file:
if re.search('SNAP.*IS-', line):
ok = True
if ok:
output += line
print(output)

Related Links

C++ Classes and Python Objects [duplicate]
How to use Java/Scala function from an action or a transformation?
Only ImportError when running file directly [duplicate]
Python Stringcompare
Unable to install packages using pip in virtualenv
Django Render function: template not printing variable
Fastest way to set a dictionary value in python when greater than current key value
Returning values from dictionary in python
How do I print a range of lines after a specific pattern into separate files when this pattern appears several times in an input file
Get more result from Github search
Python : Speed up Reverse DNS lookup
Clean way of structuring ctypes class
Django: How to setup Apache for a memory demanding application with very few users?
Animate a python pyplot by moving a point plotted via scatter
Get_all_values from Google Spreadsheet
Accelerating matplotlib figure update

Categories

HOME
security
jenkins-plugins
jar
antivirus
boost-thread
fparsec
json-ld
d
chaiscript
flyway
urlencode
facebook-javascript-sdk
keystore
android-source
xlsx
tee
mongodb-query
medium-editor
ssms-2016
internet-explorer-8
vs2017
linq-to-sql
distance
sonarqube-msbuild-runner
fop
avplayeritem
nsmenuitem
tampermonkey
websauna
xor
percentage
hawtio
android-download-manager
wsadmin
nashorn
median
startapp
number-theory
persistent
tcserver
gce
efxclipse
xib
debugdiag
uiprogressview
pagekit
gemini
distribute
group-concat
flexjson
fortran90
portfolio
servlet-3.0
acm
hateoas
futuretask
programming-paradigms
divide-by-zero
debian-based
utf-32
left-recursion
docopt
suffix-array
sid
bullet
delphi-xe3
dvcs
jstack
operations
logentries
goinstant
manage.py
sharpmap
awesomeprint
vertical-scrolling
trailing-slash
transitive-closure-table
android-4.0
osi
sunspot-rails
cadisplaylink
scrollto
unions
selectonemenu
nemerle
paintcomponent
smooth
gethashcode
.app
camtasia
iphone-sdk-3.2
microsoft.ink
soft-keyboard
internals
castle-validators

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App