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

Python qt - TableWidget update MySQL
Getting black plots with plt.imshow after multiplying image array by a scalar
Updating a value in a Pandas dataframe seems to update all dataframes
4 entry box nummeric keypad
os.walk for loop not executing [duplicate]
python matplotlib polar plot
Django - not showing properly one of my tables in template
Datalab: How to export Big Query standard SQL query to dataframe?
Pandas: count difference between dates
Skip one line in .csv file by using genfromtxt function in python
Dealing with NaNs in Pandas
Python Script? - Logging serial output from arduino
Iterate links from selenium into bs4 and print stripped strings
Query for only part of objects of related_name (from ForeignKey)
How to fill in missing sequence lines in a TSV file
Django website optimization: Too many calls to core python functions?

Categories

HOME
xbox-live
rest
project-management
winapi
angular-cli
voip
apollo
adsense
tumblr
jpanel
internet-explorer-11
metronic
criteria
x11
outlook-web-addins
public-key-encryption
cloudflare
riak
opera-mini
android-toolbar
eclipselink
ejbca
ios10.3
prediction
onsen-ui
piwik
extractor
ab-initio
data-conversion
positioning
superagent
helix-3d-toolkit
expand
backup-strategies
skygear
rhomobile
lego
nlb
vmd
sendgrid-templates
tripwire
aqgridview
chown
drawingarea
windows-firewall
dwarf
component-pascal
struts-layout
crystal-reports-8.5
rpostgresql
mcrypt
static-code-analysis
search-box
qos
sqldatareader
ruby-2.0
brython
contour
clob
selecteditem
django-south
asp.net-mvc-partialview
axes
license-key
test-class
android-viewholder
function-fitting
data-import
ninject.web.mvc
urn
crash-dumps
lines-of-code
pgm
google-hadoop
moai
jquery-autocomplete
tcpreplay
omnifocus
method-overriding
neoload
picat
help-viewer
ksoap2
android-json-rpc
visual-studio-addins
sanitization
cdata
installshield-2009
threadx
xml-libxml
transitive-closure-table
cgpath
git-filter-branch
linkedhashset
unions
concurrent-programming
jquery-selectbox
galaxy-tab
serp
authenticode
for-xml-path
server-error
audiostreamer
symbol-server
svn-hooks
requestfactory
unreachable-code
delegatecommand
visual-c++-2008-express
oggvorbis
soft-keyboard

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